@@ -2076,6 +2076,7 @@ int socket_getpeername(const uint8_t command[], uint8_t response[])
20762076#include " Preferences.h"
20772077
20782078Preferences preferences;
2079+ const char PREF_TAG[] = " preferences" ;
20792080
20802081int pref_begin (const uint8_t command[], uint8_t response[])
20812082{
@@ -2100,7 +2101,9 @@ int pref_begin(const uint8_t command[], uint8_t response[])
21002101 const uint8_t * command_ptr = &command[3 ];
21012102
21022103 if (nargs < 1 && nargs > 3 ) {
2103- // TODO error
2104+ ESP_LOGE (PREF_TAG, " Prefrences begin wrong number of arguments" );
2105+ response[4 ] = 255 ;
2106+ goto error;
21042107 }
21052108
21062109 memset (store_name, 0x00 , sizeof (store_name));
@@ -2125,12 +2128,12 @@ int pref_begin(const uint8_t command[], uint8_t response[])
21252128 partition_label_ptr = command_ptr;
21262129 }
21272130
2131+ response[4 ] = preferences.begin (store_name, readonly, (char *)partition_label_ptr) ? 0 : 1 ;
2132+
2133+ error:
21282134 response[2 ] = 1 ; // number of parameters
21292135 response[3 ] = 1 ; // length of first parameter
2130- // result of Preferences begin operation
2131- response[4 ] = preferences.begin (store_name, readonly, (char *)partition_label_ptr) ? 1 : 0 ;
2132- // response has to start ad position 2, and has to take into account
2133- // 0xee that is put after the function being called
2136+
21342137 return 6 ;
21352138}
21362139
@@ -2151,7 +2154,7 @@ int pref_clear(const uint8_t command[], uint8_t response[])
21512154
21522155 response[2 ] = 1 ; // number of parameters
21532156 response[3 ] = 1 ; // length of first parameter
2154- response[4 ] = preferences.clear () ? 1 : 0 ; // result of Preferences clear operation
2157+ response[4 ] = preferences.clear () ? 0 : 1 ; // result of Preferences clear operation
21552158
21562159 // response has to start ad position 2, and has to take into account
21572160 // 0xee that is put after the function being called
@@ -2174,16 +2177,19 @@ int pref_remove(const uint8_t command[], uint8_t response[])
21742177 const uint8_t * command_ptr = &command[3 ];
21752178
21762179 if (nargs != 1 ) {
2177- // TODO error
2180+ ESP_LOGE (PREF_TAG, " Prefrences remove wrong number of arguments" );
2181+ response[4 ] = 255 ;
2182+ goto error;
21782183 }
21792184
21802185 memset (key, 0x00 , sizeof (key));
21812186 memcpy (key, command_ptr+1 , *command_ptr);
21822187 key[*command_ptr] = ' \0 ' ;
21832188
2189+ response[4 ] = preferences.remove (key) ? 0 : 1 ; // result of Preferences end operation
2190+ error:
21842191 response[2 ] = 1 ; // number of parameters
21852192 response[3 ] = 1 ; // length of first parameter
2186- response[4 ] = preferences.remove (key) ? 1 : 0 ; // result of Preferences end operation
21872193
21882194 // response has to start ad position 2, and has to take into account
21892195 // 0xee that is put after the function being called
@@ -2209,7 +2215,11 @@ int pref_len(const uint8_t command[], uint8_t response[])
22092215 uint32_t len = 0 ;
22102216
22112217 if (nargs != 1 ) {
2212- // TODO error
2218+ ESP_LOGE (PREF_TAG, " Prefrences length wrong number of arguments" );
2219+ response[2 ] = 1 ;
2220+ response[3 ] = 1 ;
2221+ response[4 ] = 255 ;
2222+ return 6 ;
22132223 }
22142224
22152225 memset (key, 0x00 , sizeof (key));
@@ -2222,10 +2232,10 @@ int pref_len(const uint8_t command[], uint8_t response[])
22222232 response[3 ] = 4 ; // length of first parameter
22232233
22242234 // write the result in big endian into the response buffer
2225- response[7 ] = (len >> 24 ) & 0xff ;
2226- response[6 ] = (len >> 16 ) & 0xff ;
2227- response[5 ] = (len >> 8 ) & 0xff ;
22282235 response[4 ] = (len >> 0 ) & 0xff ;
2236+ response[5 ] = (len >> 8 ) & 0xff ;
2237+ response[6 ] = (len >> 16 ) & 0xff ;
2238+ response[7 ] = (len >> 24 ) & 0xff ;
22292239
22302240 return 9 ;
22312241}
@@ -2244,10 +2254,10 @@ int pref_stat(const uint8_t command[], uint8_t response[])
22442254 response[3 ] = 4 ; // length of first parameter
22452255
22462256 // write the result in big endian into the response buffer
2247- response[7 ] = (res >> 24 ) & 0xff ;
2248- response[6 ] = (res >> 16 ) & 0xff ;
2249- response[5 ] = (res >> 8 ) & 0xff ;
22502257 response[4 ] = (res >> 0 ) & 0xff ;
2258+ response[5 ] = (res >> 8 ) & 0xff ;
2259+ response[6 ] = (res >> 16 ) & 0xff ;
2260+ response[7 ] = (res >> 24 ) & 0xff ;
22512261
22522262 return 9 ;
22532263}
@@ -2278,8 +2288,12 @@ int pref_put(const uint8_t command[], uint8_t response[])
22782288 // restricting the return as 32 bit integer as it is enough
22792289 size_t res = 0 ;
22802290
2281- if (nargs != 2 && nargs != 3 ) {
2282- // TODO error
2291+ if (nargs != 3 ) {
2292+ ESP_LOGE (PREF_TAG, " Prefrences put wrong number of arguments" );
2293+ response[2 ] = 1 ;
2294+ response[3 ] = 1 ;
2295+ response[4 ] = 255 ;
2296+ return 6 ;
22832297 }
22842298
22852299 memset (key, 0x00 , sizeof (key));
@@ -2289,10 +2303,6 @@ int pref_put(const uint8_t command[], uint8_t response[])
22892303 // next argument
22902304 command_ptr += *command_ptr + 1 ;
22912305
2292- if (*command_ptr >= PT_INVALID) {
2293- // TODO error
2294- }
2295-
22962306 command_ptr++; // The first byte contains the length of the parameter, which is 1
22972307 PreferenceType type = (PreferenceType)*command_ptr;
22982308 command_ptr++;
@@ -2341,17 +2351,20 @@ int pref_put(const uint8_t command[], uint8_t response[])
23412351 break ;
23422352 case PT_INVALID:
23432353 default :
2344- // TODO error
2345- break ;
2354+ ESP_LOGE (PREF_TAG, " Prefrences put invalid type" );
2355+ response[2 ] = 1 ;
2356+ response[3 ] = 1 ;
2357+ response[4 ] = 254 ;
2358+ return 6 ;
23462359 }
23472360
23482361 response[2 ] = 1 ; // response nargs
23492362 response[3 ] = 4 ; // length of first parameter
23502363
2351- response[7 ] = (res >> 24 ) & 0xff ;
2352- response[6 ] = (res >> 16 ) & 0xff ;
2353- response[5 ] = (res >> 8 ) & 0xff ;
23542364 response[4 ] = (res >> 0 ) & 0xff ;
2365+ response[5 ] = (res >> 8 ) & 0xff ;
2366+ response[6 ] = (res >> 16 ) & 0xff ;
2367+ response[7 ] = (res >> 24 ) & 0xff ;
23552368
23562369 return 9 ;
23572370}
@@ -2379,7 +2392,10 @@ int pref_get(const uint8_t command[], uint8_t response[])
23792392 uint32_t res=0 ;
23802393
23812394 if (nargs != 2 ) {
2382- // TODO error
2395+ ESP_LOGE (PREF_TAG, " Prefrences put wrong number of arguments" );
2396+ response[2 ] = 1 ;
2397+ response[3 ] = 0 ;
2398+ return 5 ;
23832399 }
23842400
23852401 memset (key, 0x00 , sizeof (key));
@@ -2392,9 +2408,6 @@ int pref_get(const uint8_t command[], uint8_t response[])
23922408 command_ptr++; // The first byte contains the length of the parameter, which is 1
23932409 PreferenceType type = static_cast <PreferenceType>(*command_ptr);
23942410
2395- if (type >= PT_INVALID) {
2396- // TODO error
2397- }
23982411 command_ptr++;
23992412
24002413 switch (type) {
@@ -2438,8 +2451,10 @@ int pref_get(const uint8_t command[], uint8_t response[])
24382451 goto array_return;
24392452 case PT_INVALID:
24402453 default :
2441- // TODO error
2442- break ;
2454+ ESP_LOGE (PREF_TAG, " Prefrences put invalid type" );
2455+ response[2 ] = 1 ;
2456+ response[3 ] = 0 ;
2457+ return 5 ;
24432458 }
24442459
24452460 // fill the response buffer
0 commit comments