@@ -63,28 +63,44 @@ static term nif_esp_nvs_get_binary(Context *ctx, int argc, term argv[])
6363 switch (err ) {
6464 case ESP_OK :
6565 break ;
66- case ESP_ERR_NVS_NOT_FOUND :
66+ case ESP_ERR_NVS_NOT_FOUND : {
6767 TRACE ("No such namespace. namespace='%s'\n" , namespace );
68- return UNDEFINED_ATOM ;
68+ if (UNLIKELY (memory_ensure_free (ctx , TUPLE_SIZE (2 )) != MEMORY_GC_OK )) {
69+ RAISE_ERROR (OUT_OF_MEMORY_ATOM );
70+ }
71+ term error_tuple = term_alloc_tuple (2 , & ctx -> heap );
72+ term ns_not_found = globalcontext_make_atom (ctx -> global , ATOM_STR ("\x13" , "namespace_not_found" ));
73+ term_put_tuple_element (error_tuple , 0 , OK_ATOM );
74+ term_put_tuple_element (error_tuple , 1 , ns_not_found );
75+ return error_tuple ;
76+ }
6977 default :
70- fprintf ( stderr , "Unable to open NVS. namespace '%s' err=%i\n" , namespace , err );
71- RAISE_ERROR (term_from_int ( err ));
78+ TRACE ( "Unable to open NVS. namespace '%s' err=%i\n" , namespace , err );
79+ RAISE_ERROR (esp_err_to_term ( ctx -> global , err ));
7280 }
7381
7482 size_t size = 0 ;
7583 err = nvs_get_blob (nvs , key , NULL , & size );
7684 switch (err ) {
7785 case ESP_OK :
7886 break ;
79- case ESP_ERR_NVS_NOT_FOUND :
87+ case ESP_ERR_NVS_NOT_FOUND : {
8088 TRACE ("No such entry. namespace='%s' key='%s'\n" , namespace , key );
81- return UNDEFINED_ATOM ;
89+ if (UNLIKELY (memory_ensure_free (ctx , TUPLE_SIZE (2 )) != MEMORY_GC_OK )) {
90+ RAISE_ERROR (OUT_OF_MEMORY_ATOM );
91+ }
92+ term error_tuple = term_alloc_tuple (2 , & ctx -> heap );
93+ term not_found = globalcontext_make_atom (ctx -> global , ATOM_STR ("\x9" , "not_found" ));
94+ term_put_tuple_element (error_tuple , 0 , OK_ATOM );
95+ term_put_tuple_element (error_tuple , 1 , not_found );
96+ return error_tuple ;
97+ }
8298 default :
83- fprintf ( stderr , "Unable to get NVS blob size. namespace '%s' key='%s' err=%i\n" , namespace , key , err );
84- RAISE_ERROR (term_from_int ( err ));
99+ TRACE ( "Unable to get NVS blob size. namespace '%s' key='%s' err=%i\n" , namespace , key , err );
100+ RAISE_ERROR (esp_err_to_term ( ctx -> global , err ));
85101 }
86102
87- if (UNLIKELY (memory_ensure_free (ctx , term_binary_heap_size (size )) != MEMORY_GC_OK )) {
103+ if (UNLIKELY (memory_ensure_free (ctx , term_binary_heap_size (size ) + TUPLE_SIZE ( 2 ) ) != MEMORY_GC_OK )) {
88104 TRACE ("Unabled to ensure free space for binary. namespace='%s' key='%s' size=%i\n" , namespace , key , size );
89105 RAISE_ERROR (OUT_OF_MEMORY_ATOM );
90106 }
@@ -95,7 +111,10 @@ static term nif_esp_nvs_get_binary(Context *ctx, int argc, term argv[])
95111 switch (err ) {
96112 case ESP_OK :
97113 TRACE ("Found data for key. namespace='%s' key='%s' size='%i'\n" , namespace , key , size );
98- return binary ;
114+ term result_tuple = term_alloc_tuple (2 , & ctx -> heap );
115+ term_put_tuple_element (result_tuple , 0 , OK_ATOM );
116+ term_put_tuple_element (result_tuple , 1 , binary );
117+ return result_tuple ;
99118 default :
100119 fprintf (stderr , "Unable to get NVS blob. namespace='%s' key='%s' err=%i\n" , namespace , key , err );
101120 RAISE_ERROR (term_from_int (err ));
0 commit comments