hc
2024-08-12 233ab1bd4c5697f5cdec94e60206e8c6ac609b4c
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
 
 
#include <sys/types.h>
#include <sys/ipc.h>
 
#include <ctype.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
 
//#include "shm.h"
#include "script.h"
#include "debug.h"
 
 
#define DATA_TYPE_SINGLE_WORD           1
#define DATA_TYPE_STRING                2
#define DATA_TYPE_MULTI_WORD            3
#define DATA_TYPE_GPIO                  4
 
struct script_head
{
    int mainkey_cnt;
    int version[3];
};
 
struct script_mainkey
{
    char name[32];
    int length;
    int offset;
};
 
struct script_subkey
{
    char name[32];
    int offset;
    int pattern;
};
 
static char *script_buf = NULL;
static int mainkey_cnt = 0;
 
int init_script(char* buf)
{
#if 0
    script_buf = shmat(shmid, 0, 0);
    if (script_buf == (void *)-1) {
        db_error("script: attach the share memory segment failed(%s)\n", 
                strerror(errno));
        return -1;
    }
#else
   script_buf = buf;
#endif
    struct script_head *head = (struct script_head *)script_buf;
 
    mainkey_cnt = head->mainkey_cnt;
    db_debug("script: main key count #%d\n", mainkey_cnt);
    db_msg("script: V%d.%d.%d\n", head->version[0], 
            head->version[1], head->version[2]);
 
    return 0;
}
 
void deinit_script(void)
{
    if (script_buf) {
       // shmdt(script_buf);
        script_buf = NULL;
    }
 
    mainkey_cnt = 0;
}
 
int script_mainkey_cnt(void)
{
    return mainkey_cnt;
}
 
int script_mainkey_name(int idx, char *name)
{
    struct script_mainkey *mainkey;
 
    if (script_buf == NULL) {
        return -1;
    }
 
    mainkey = (struct script_mainkey *)(script_buf + sizeof(struct script_head) + 
            idx * sizeof(struct script_mainkey));
 
    strncpy(name, mainkey->name, 32);
 
    return 0;
}
 
int script_fetch(char *main_name, char *sub_name, int value[], int count)
{
    int i, j;
    struct script_mainkey *mainkey;
    struct script_subkey *subkey;
    int pattern, wordcnt;
    
    /* check parameter */
    if (script_buf == NULL) {
        return -1;
    }
 
    if (main_name == NULL || sub_name == NULL) {
        return -1;
    }
 
    if (value == NULL) {
        return -1;
    }
 
    memset(value, 0, sizeof(int) * count);
 
    /* search main key */
    for (i = 0; i < mainkey_cnt; i++) {
        mainkey = (struct script_mainkey *)(script_buf + 
                sizeof(struct script_head) + i * sizeof(struct script_mainkey));
        if (strncmp(mainkey->name, main_name, 31)) {
            continue;
        }
 
        /* search sub key */
        for (j = 0; j < mainkey->length; j++) {
            subkey = (struct script_subkey *)(script_buf + 
                     (mainkey->offset << 2) + 
                     (j * sizeof(struct script_subkey)));
            if (strncmp(subkey->name, sub_name, 31)) {
                continue;
            }
 
            pattern = (subkey->pattern >> 16) & 0xffff;
            wordcnt = subkey->pattern & 0xffff;
 
            switch (pattern) {
                case DATA_TYPE_SINGLE_WORD:
                    value[0] = *(int *)(script_buf + (subkey->offset << 2));
                    break;
 
                case DATA_TYPE_STRING:
                    if (wordcnt == 0)
                        break;
 
                    if (wordcnt > count)
                        wordcnt = count;
 
                    memcpy((char *)value, script_buf + (subkey->offset << 2),
                            wordcnt << 2);
                    break;
 
                case DATA_TYPE_GPIO:
                    db_debug("%s: L%d fix me\n", __func__, __LINE__);
                    break;
 
                default:
                    db_debug("%s: L%d fix me\n", __func__, __LINE__);
                    break;
            }
 
            return 0;
        }
    }
 
    return -1;
}