summaryrefslogtreecommitdiffstats
path: root/views/transform.c
blob: 02edddf8ff0384af471b5e39018a69029b933fbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#define _PCILIB_VIEW_TRANSFORM_C

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

#include "version.h"
#include "model.h"
#include "transform.h"
#include "py.h"
#include "error.h"

static int pcilib_transform_view_read(pcilib_t *ctx, pcilib_view_context_t *view_ctx, pcilib_register_value_t regval, pcilib_value_t *val) {
    const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
    pcilib_transform_view_description_t *v = (pcilib_transform_view_description_t*)(model_info->views[view_ctx->view]);

	int err;

	err = pcilib_set_value_from_register_value(ctx, val, regval);
	if (err) return err;

	if(v->module)
		return err = pcilib_script_run_func(ctx, v->module,
		                                    "read_from_register", val);
	else
		return  pcilib_py_eval_string(ctx, v->read_from_reg, val);
}

static int pcilib_transform_view_write(pcilib_t *ctx, pcilib_view_context_t *view_ctx, pcilib_register_value_t *regval, const pcilib_value_t *val) {
	
    const pcilib_model_description_t *model_info = pcilib_get_model_description(ctx);
    pcilib_transform_view_description_t *v = (pcilib_transform_view_description_t*)(model_info->views[view_ctx->view]);

	int err = 0;
	
	pcilib_value_t val_copy = {0};
	err = pcilib_copy_value(ctx, &val_copy, val);
	if (err) return err;


	if(v->module)
		err = pcilib_script_run_func(ctx, v->module,
		                             "write_to_register", &val_copy);
	else
		err = pcilib_py_eval_string(ctx, v->write_to_reg, &val_copy);
		
	if (err) return err;
	
	*regval = pcilib_get_value_as_register_value(ctx, &val_copy, &err);
	return err;
}

void pcilib_transform_view_free_description (pcilib_t *ctx, pcilib_view_description_t *view)
{
	pcilib_transform_view_description_t *v = (pcilib_transform_view_description_t*)(view);
	
	if(v->module)
        pcilib_py_free_script(ctx, v->module);
        
    free(v);
}

pcilib_view_context_t * pcilib_transform_view_init(pcilib_t *ctx, const pcilib_view_description_t *desc)
{
	pcilib_transform_view_description_t *v_desc = (pcilib_transform_view_description_t*)desc;
	
	if(v_desc->module)
	{	
		pcilib_access_mode_t mode = 0;
		
		int err = pcilib_py_init_script(ctx, v_desc->module);
		if(err)
		{
			pcilib_error("Failed init script module (%s) - error %i",
			             v_desc->module, err);
			return NULL;
		}
		err = pcilib_py_get_transform_script_properties(ctx, v_desc->module,
                                                        &mode);
        if(err)
		{
			pcilib_error("Failed get transform script properties (%s) - error %i",
			             v_desc->module, err);
			return NULL;
		}                                               
		
		v_desc->base.mode |= PCILIB_REGISTER_RW;
		mode |= PCILIB_REGISTER_INCONSISTENT;
		v_desc->base.mode &= mode;
	}
	
	pcilib_view_context_t *view_ctx;
	view_ctx = (pcilib_view_context_t*)malloc(sizeof(pcilib_view_context_t));
    if (view_ctx) memset(view_ctx, 0, sizeof(pcilib_view_context_t));
    
    return view_ctx;
}

const pcilib_view_api_description_t pcilib_transform_view_api =
  { PCILIB_VERSION, sizeof(pcilib_transform_view_description_t), pcilib_transform_view_init, NULL, pcilib_transform_view_free_description, pcilib_transform_view_read,  pcilib_transform_view_write };