xref: /OK3568_Linux_fs/external/rk_pcba_test/pcba_minui/script.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 
2 
3 #include <sys/types.h>
4 #include <sys/ipc.h>
5 
6 #include <ctype.h>
7 #include <errno.h>
8 #include <string.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11 
12 //#include "shm.h"
13 #include "script.h"
14 #include "debug.h"
15 
16 
17 #define DATA_TYPE_SINGLE_WORD           1
18 #define DATA_TYPE_STRING                2
19 #define DATA_TYPE_MULTI_WORD            3
20 #define DATA_TYPE_GPIO                  4
21 
22 struct script_head
23 {
24     int mainkey_cnt;
25     int version[3];
26 };
27 
28 struct script_mainkey
29 {
30     char name[32];
31     int length;
32     int offset;
33 };
34 
35 struct script_subkey
36 {
37     char name[32];
38     int offset;
39     int pattern;
40 };
41 
42 static char *script_buf = NULL;
43 static int mainkey_cnt = 0;
44 
init_script(char * buf)45 int init_script(char* buf)
46 {
47 #if 0
48     script_buf = shmat(shmid, 0, 0);
49     if (script_buf == (void *)-1) {
50         db_error("script: attach the share memory segment failed(%s)\n",
51                 strerror(errno));
52         return -1;
53     }
54 #else
55    script_buf = buf;
56 #endif
57     struct script_head *head = (struct script_head *)script_buf;
58 
59     mainkey_cnt = head->mainkey_cnt;
60     db_debug("script: main key count #%d\n", mainkey_cnt);
61     db_msg("script: V%d.%d.%d\n", head->version[0],
62             head->version[1], head->version[2]);
63 
64     return 0;
65 }
66 
deinit_script(void)67 void deinit_script(void)
68 {
69     if (script_buf) {
70        // shmdt(script_buf);
71         script_buf = NULL;
72     }
73 
74     mainkey_cnt = 0;
75 }
76 
script_mainkey_cnt(void)77 int script_mainkey_cnt(void)
78 {
79     return mainkey_cnt;
80 }
81 
script_mainkey_name(int idx,char * name)82 int script_mainkey_name(int idx, char *name)
83 {
84     struct script_mainkey *mainkey;
85 
86     if (script_buf == NULL) {
87         return -1;
88     }
89 
90     mainkey = (struct script_mainkey *)(script_buf + sizeof(struct script_head) +
91             idx * sizeof(struct script_mainkey));
92 
93     strncpy(name, mainkey->name, 32);
94 
95     return 0;
96 }
97 
script_fetch(char * main_name,char * sub_name,int value[],int count)98 int script_fetch(char *main_name, char *sub_name, int value[], int count)
99 {
100     int i, j;
101     struct script_mainkey *mainkey;
102     struct script_subkey *subkey;
103     int pattern, wordcnt;
104 
105     /* check parameter */
106     if (script_buf == NULL) {
107         return -1;
108     }
109 
110     if (main_name == NULL || sub_name == NULL) {
111         return -1;
112     }
113 
114     if (value == NULL) {
115         return -1;
116     }
117 
118     memset(value, 0, sizeof(int) * count);
119 
120     /* search main key */
121     for (i = 0; i < mainkey_cnt; i++) {
122         mainkey = (struct script_mainkey *)(script_buf +
123                 sizeof(struct script_head) + i * sizeof(struct script_mainkey));
124         if (strncmp(mainkey->name, main_name, 31)) {
125             continue;
126         }
127 
128         /* search sub key */
129         for (j = 0; j < mainkey->length; j++) {
130             subkey = (struct script_subkey *)(script_buf +
131                      (mainkey->offset << 2) +
132                      (j * sizeof(struct script_subkey)));
133             if (strncmp(subkey->name, sub_name, 31)) {
134                 continue;
135             }
136 
137             pattern = (subkey->pattern >> 16) & 0xffff;
138             wordcnt = subkey->pattern & 0xffff;
139 
140             switch (pattern) {
141                 case DATA_TYPE_SINGLE_WORD:
142                     value[0] = *(int *)(script_buf + (subkey->offset << 2));
143                     break;
144 
145                 case DATA_TYPE_STRING:
146                     if (wordcnt == 0)
147                         break;
148 
149                     if (wordcnt > count)
150                         wordcnt = count;
151 
152                     memcpy((char *)value, script_buf + (subkey->offset << 2),
153                             wordcnt << 2);
154                     break;
155 
156                 case DATA_TYPE_GPIO:
157                     db_debug("%s: L%d fix me\n", __func__, __LINE__);
158                     break;
159 
160                 default:
161                     db_debug("%s: L%d fix me\n", __func__, __LINE__);
162                     break;
163             }
164 
165             return 0;
166         }
167     }
168 
169     return -1;
170 }
171