Skip to content

Commit 13fe960

Browse files
author
Bs0Dd
committed
Methods list parsing, new program - ldevmet
1 parent 47b9598 commit 13fe960

File tree

5 files changed

+175
-2
lines changed

5 files changed

+175
-2
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ add_executable(fimexu fimexu.c)
3131

3232
add_executable(seplay seplay.c)
3333

34+
add_executable(ldevmet ldevmet.c)
35+
3436
add_executable(lshldev lshldev.c)
3537

3638
target_link_libraries(redstone coverett)
@@ -39,5 +41,7 @@ target_link_libraries(fimexu coverett)
3941

4042
target_link_libraries(seplay coverett)
4143

44+
target_link_libraries(ldevmet coverett)
45+
4246
target_link_libraries(lshldev coverett)
4347

cJSON

Submodule cJSON updated 1 file

coverett.c

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ device_t findDev(bus_t bus, char* name){
107107
return proxyDevByList(bus, list, id);
108108
}
109109

110-
list_t getMethods(device_t* device){ //TODO: Methods parsing!!!
110+
list_t getMethods(device_t* device){
111111
if (!existsStatus(device)) return (list_t){CO_ERROR, NULL};
112112
char reqbody[65];
113113
sprintf(reqbody, "{\"type\":\"methods\",\"data\":\"%s\"}", device->devId);
@@ -120,6 +120,71 @@ list_t getMethods(device_t* device){ //TODO: Methods parsing!!!
120120
return (list_t){CO_ERROR, NULL};
121121
}
122122

123+
method_t* parseMethods(list_t list, int* meths){
124+
if (list.type != CO_METHODS) {
125+
return NULL;
126+
}
127+
*meths = cJSON_GetArraySize(list.body);
128+
if (*meths < 1) {
129+
return NULL;
130+
}
131+
method_t* ret = (method_t*)calloc(*meths, sizeof(method_t));
132+
cJSON* val = NULL;
133+
cJSON* method = NULL;
134+
int i = 0;
135+
cJSON_ArrayForEach(method, list.body){
136+
val = cJSON_GetObjectItemCaseSensitive(method, "name");
137+
ret[i].name = val == NULL ? NULL : strdup(val->valuestring);
138+
val = cJSON_GetObjectItemCaseSensitive(method, "returnType");
139+
ret[i].returnType = val == NULL ? NULL : strdup(val->valuestring);
140+
val = cJSON_GetObjectItemCaseSensitive(method, "description");
141+
ret[i].description = val == NULL ? NULL : strdup(val->valuestring);
142+
val = cJSON_GetObjectItemCaseSensitive(method, "returnValueDescription");
143+
ret[i].returnValueDescription = val == NULL ? NULL : strdup(val->valuestring);
144+
cJSON* parms = cJSON_GetObjectItemCaseSensitive(method, "parameters");
145+
if (parms != NULL && cJSON_GetArraySize(parms) > 0) {
146+
ret[i].paramNum = cJSON_GetArraySize(parms);
147+
param_t* par = (param_t*)calloc(cJSON_GetArraySize(parms), sizeof(param_t));
148+
cJSON* parm = NULL;
149+
int j = 0;
150+
cJSON_ArrayForEach(parm, parms){
151+
val = cJSON_GetObjectItemCaseSensitive(parm, "name");
152+
par[j].name = val == NULL ? NULL : strdup(val->valuestring);
153+
val = cJSON_GetObjectItemCaseSensitive(parm, "type");
154+
par[j].type = val == NULL ? NULL : strdup(val->valuestring);
155+
val = cJSON_GetObjectItemCaseSensitive(parm, "description");
156+
par[j].description = val == NULL ? NULL : strdup(val->valuestring);
157+
j++;
158+
}
159+
ret[i].parameters = par;
160+
} else{
161+
ret[i].parameters = NULL;
162+
}
163+
i++;
164+
}
165+
return ret;
166+
}
167+
168+
void deleteParsedMethods(method_t* methods, int meths){
169+
if (methods == NULL) {
170+
return;
171+
}
172+
for (int i = 0; i < meths; i++){
173+
if (methods[i].name != NULL) free(methods[i].name);
174+
if (methods[i].returnType != NULL) free(methods[i].returnType);
175+
if (methods[i].description != NULL) free(methods[i].description);
176+
if (methods[i].returnValueDescription != NULL) free(methods[i].returnValueDescription);
177+
for (int j = 0; j < methods[i].paramNum; j++){
178+
if (methods[i].parameters[j].name != NULL) free(methods[i].parameters[j].name);
179+
if (methods[i].parameters[j].type != NULL) free(methods[i].parameters[j].type);
180+
if (methods[i].parameters[j].description != NULL) free(methods[i].parameters[j].description);
181+
}
182+
if (methods[i].parameters != NULL) free(methods[i].parameters);
183+
}
184+
free(methods);
185+
methods = NULL;
186+
}
187+
123188
result_t uniInvoke(device_t* dev, char* method, double* numvals, char** strvals, int total, cotypes_t* order){
124189
if (!dev->exists || !existsStatus(dev)) return (result_t){CO_ERROR, 0, NULL, NULL, "Device is no longer available"};
125190
cJSON* reqbody = cJSON_CreateObject();

coverett.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@ typedef struct{
2828
char* errString;
2929
} list_t;
3030

31+
typedef struct{
32+
char* name;
33+
char* description;
34+
char* type;
35+
} param_t;
36+
37+
typedef struct{
38+
char* name;
39+
char* returnType;
40+
char* description;
41+
char* returnValueDescription;
42+
int paramNum;
43+
param_t* parameters;
44+
} method_t;
45+
3146
typedef struct{
3247
int exists;
3348
const char* devType;
@@ -163,6 +178,24 @@ device_t findDev(bus_t bus, char* name);
163178
*/
164179
list_t getMethods(device_t* device);
165180

181+
/**
182+
* @brief Parse methods list.
183+
*
184+
* @param[in] list List of type CO_METHODS.
185+
* @param[in] meths Pointer to an integer where the total number of methods will be placed.
186+
*
187+
* @return Array of method_t structures or NULL.
188+
*/
189+
method_t* parseMethods(list_t list, int* meths);
190+
191+
/**
192+
* @brief Delete array of method_t structures (free memory).
193+
*
194+
* @param[in] methods Array of method_t structures.
195+
* @param[in] meths Pointer to an integer where the total number of methods will be placed.
196+
*/
197+
void deleteParsedMethods(method_t* methods, int meths);
198+
166199
/**
167200
* @brief Unversal invoker for methods.
168201
*

ldevmet.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include <stdio.h>
2+
#include "coverett.h"
3+
4+
int main(int argc, char* argv[]){
5+
if (argc < 2){
6+
puts("List DEVice METhods");
7+
puts("Usage: ldevmet <devname>");
8+
return 0;
9+
}
10+
11+
bus_t stdbus = openBus("/dev/hvc0");
12+
device_t dev = findDev(stdbus, argv[1]);
13+
if (!dev.exists){
14+
fprintf(stderr, "Device \"%s\" is not found.\n", argv[1]);
15+
closeBus(stdbus);
16+
return -1;
17+
}
18+
19+
list_t meth = getMethods(&dev);
20+
if (meth.type == CO_ERROR){
21+
fputs("Failed to get methods list.\n", stderr);
22+
closeBus(stdbus);
23+
return -1;
24+
}
25+
closeBus(stdbus);
26+
27+
int msiz;
28+
method_t* mes = parseMethods(meth, &msiz);
29+
deleteList(&meth);
30+
31+
if (mes == NULL){
32+
fputs("Failed to parse methods list.\n", stderr);
33+
return -1;
34+
}
35+
36+
printf("Methods for device \"%s\":\n", argv[1]);
37+
for (int i = 0; i < msiz; i++){
38+
printf(" - %s(",mes[i].name);
39+
int narg = 1;
40+
for (int j = 0; j < mes[i].paramNum; j++){
41+
if (mes[i].parameters[j].name){
42+
printf("%s: ", mes[i].parameters[j].name);
43+
} else{
44+
printf("arg%d: ", narg++);
45+
}
46+
printf("%s", mes[i].parameters[j].type);
47+
if (j+1 < mes[i].paramNum) printf(", ");
48+
}
49+
printf("): %s\n", mes[i].returnType);
50+
if (mes[i].description != NULL){
51+
printf(" %s", mes[i].description);
52+
if (mes[i].description[strlen(mes[i].description)-1] != '\n') printf("\n");
53+
}
54+
narg = 1;
55+
for (int j = 0; j < mes[i].paramNum; j++){
56+
if (mes[i].parameters[j].description != NULL){
57+
if (mes[i].parameters[j].name){
58+
printf(" '%s' - ", mes[i].parameters[j].name);
59+
} else{
60+
printf(" 'arg%d' - ", narg++);
61+
}
62+
printf("%s\n", mes[i].parameters[j].description);
63+
}
64+
}
65+
printf("\n");
66+
}
67+
68+
deleteParsedMethods(mes, msiz);
69+
70+
return 0;
71+
}

0 commit comments

Comments
 (0)