/alps/pcitool

To get this branch, use:
bzr branch http://suren.me/webbzr/alps/pcitool

« back to all changes in this revision

Viewing changes to pcilib/pcipywrap.c

  • Committer: Vasilii Chernov
  • Date: 2016-02-03 15:48:45 UTC
  • mto: This revision was merged to the branch mainline in revision 353.
  • Revision ID: vchernov@inr.ru-20160203154845-bnyvhpe6fnirhrka
Add python wrap for pcilib

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "pcilib.h"
 
2
 
 
3
//Remove unused headers
 
4
#include <stdio.h>
 
5
#include <stdlib.h>
 
6
#include <string.h>
 
7
#include <strings.h>
 
8
#include <stdint.h>
 
9
#include <stdarg.h>
 
10
#include <fcntl.h>
 
11
#include <unistd.h>
 
12
#include <sys/time.h>
 
13
#include <sys/ioctl.h>
 
14
#include <sys/mman.h>
 
15
#include <errno.h>
 
16
#include <alloca.h>
 
17
#include <arpa/inet.h>
 
18
#include <sys/types.h>
 
19
#include <sys/stat.h>
 
20
#include <dirent.h>
 
21
#include <pthread.h>
 
22
#include <signal.h>
 
23
#include <dlfcn.h>
 
24
 
 
25
#include <getopt.h>
 
26
 
 
27
#include <fastwriter.h>
 
28
 
 
29
#include "pcitool/sysinfo.h"
 
30
#include "pcitool/formaters.h"
 
31
 
 
32
#include "views/transform.h"
 
33
#include "views/enum.h"
 
34
#include "pci.h"
 
35
#include "plugin.h"
 
36
#include "config.h"
 
37
#include "tools.h"
 
38
#include "kmem.h"
 
39
#include "error.h"
 
40
#include "debug.h"
 
41
#include "model.h"
 
42
#include "locking.h"
 
43
 
 
44
pcilib_t* __ctx = 0;
 
45
pcilib_model_description_t *model_info = 0;
 
46
 
 
47
/*!
 
48
 * \brief присваивание указателя на устройство. Закрытая функция. Будет проходить при парсинге xml.
 
49
 * \param ctx
 
50
 */
 
51
void __initCtx(void* ctx)
 
52
{
 
53
    __ctx = ctx;
 
54
}
 
55
 
 
56
/*!
 
57
 * \brief создание хэндлера устройства, для тестирования скрипта не из программы.
 
58
 * \return
 
59
 */
 
60
void __createCtxInstance(const char *fpga_device, const char *model)
 
61
{
 
62
    __ctx = pcilib_open(fpga_device, model);
 
63
    model_info = pcilib_get_model_description(__ctx);
 
64
}
 
65
 
 
66
int read_register(const char *bank, const char *regname, void *value)
 
67
{
 
68
    int ret = pcilib_read_register(__ctx, bank, regname, (pcilib_register_value_t*)value);
 
69
    return ret;
 
70
}
 
71
 
 
72
void Error(const char *message, const char *attr, ...)
 
73
{
 
74
                printf("Catch error: %s, %s\n", message, attr);
 
75
}
 
76
 
 
77
int ReadRegister(const char *bank, const char *reg) {
 
78
    
 
79
    const char *view = NULL;
 
80
    const char *unit = NULL;
 
81
    const char *attr = NULL;
 
82
    pcilib_t *handle = __ctx;
 
83
    int i;
 
84
    int err;
 
85
    const char *format;
 
86
 
 
87
    pcilib_register_bank_t bank_id;
 
88
    pcilib_register_bank_addr_t bank_addr = 0;
 
89
 
 
90
    pcilib_register_value_t value;
 
91
 
 
92
        // Adding DMA registers
 
93
    pcilib_get_dma_description(handle);
 
94
 
 
95
    if (reg||view||attr) {
 
96
        pcilib_value_t val = {0};
 
97
        if (attr) {
 
98
            if (reg) err = pcilib_get_register_attr(handle, bank, reg, attr, &val);
 
99
            else if (view) err = pcilib_get_property_attr(handle, view, attr, &val);
 
100
            else if (bank) err = pcilib_get_register_bank_attr(handle, bank, attr, &val);
 
101
            else err = PCILIB_ERROR_INVALID_ARGUMENT;
 
102
 
 
103
            if (err) {
 
104
                if (err == PCILIB_ERROR_NOTFOUND)
 
105
                    Error("Attribute %s is not found", attr);
 
106
                else
 
107
                    Error("Error (%i) reading attribute %s", err, attr);
 
108
            }
 
109
 
 
110
            err = pcilib_convert_value_type(handle, &val, PCILIB_TYPE_STRING);
 
111
            if (err) Error("Error converting attribute %s to string", attr);
 
112
 
 
113
            printf("%s = %s", attr, val.sval);
 
114
            if ((val.unit)&&(strcasecmp(val.unit, "name")))
 
115
                printf(" %s", val.unit);
 
116
            printf(" (for %s)\n", (reg?reg:(view?view:bank)));
 
117
        } else if (view) {
 
118
            if (reg) {
 
119
                err = pcilib_read_register_view(handle, bank, reg, view, &val);
 
120
                if (err) Error("Error reading view %s of register %s", view, reg);
 
121
            } else {
 
122
                err = pcilib_get_property(handle, view, &val);
 
123
                if (err) Error("Error reading property %s", view);
 
124
            }
 
125
 
 
126
            if (unit) {
 
127
                err = pcilib_convert_value_unit(handle, &val, unit);
 
128
                if (err) {
 
129
                    if (reg) Error("Error converting view %s of register %s to unit %s", view, reg, unit);
 
130
                    else Error("Error converting property %s to unit %s", view, unit);
 
131
                }
 
132
            }
 
133
            
 
134
            err = pcilib_convert_value_type(handle, &val, PCILIB_TYPE_STRING);
 
135
            if (err) {
 
136
                if (reg) Error("Error converting view %s of register %s to string", view);
 
137
                else Error("Error converting property %s to string", view);
 
138
            }
 
139
 
 
140
            printf("%s = %s", (reg?reg:view), val.sval);
 
141
            if ((val.unit)&&(strcasecmp(val.unit, "name")))
 
142
                printf(" %s", val.unit);
 
143
            printf("\n");
 
144
        } else {
 
145
            pcilib_register_t regid = pcilib_find_register(handle, bank, reg);
 
146
            bank_id = pcilib_find_register_bank_by_addr(handle, model_info->registers[regid].bank);
 
147
            format = model_info->banks[bank_id].format;
 
148
            if (!format) format = "%lu";
 
149
            err = pcilib_read_register_by_id(handle, regid, &value);
 
150
            if (err) Error("Error reading register %s", reg);
 
151
 
 
152
            printf("%s = ", reg);
 
153
            printf(format, value);
 
154
            printf("\n");
 
155
        }
 
156
    } else {
 
157
        if (model_info->registers) {
 
158
            if (bank) {
 
159
                bank_id = pcilib_find_register_bank(handle, bank);
 
160
                bank_addr = model_info->banks[bank_id].addr;
 
161
            }
 
162
            
 
163
            printf("Registers:\n");
 
164
            for (i = 0; model_info->registers[i].bits; i++) {
 
165
                if ((model_info->registers[i].mode & PCILIB_REGISTER_R)&&((!bank)||(model_info->registers[i].bank == bank_addr))&&(model_info->registers[i].type != PCILIB_REGISTER_BITS)) { 
 
166
                    bank_id = pcilib_find_register_bank_by_addr(handle, model_info->registers[i].bank);
 
167
                    format = model_info->banks[bank_id].format;
 
168
                    if (!format) format = "%lu";
 
169
 
 
170
                    err = pcilib_read_register_by_id(handle, i, &value);
 
171
                    if (err) printf(" %s = error reading value", model_info->registers[i].name);
 
172
                    else {
 
173
                        printf(" %s = ", model_info->registers[i].name);
 
174
                        printf(format, value);
 
175
                    }
 
176
 
 
177
                    printf(" [");
 
178
                    printf(format, model_info->registers[i].defvalue);
 
179
                    printf("]");
 
180
                    printf("\n");
 
181
                }
 
182
            }
 
183
        } else {
 
184
            printf("No registers");
 
185
        }
 
186
        printf("\n");
 
187
    }
 
188
 
 
189
    return 0;
 
190
}