@@ -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+
123188result_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 ();
0 commit comments