Handling of char value

Posted on
  • During porting jswrap_nrf_bluetooth_updateServices to ESP32, I found calls to nrf relevant functions.
    ESP32 uses different walks through all of this. Most common point is use of UUIDs and handles.

    Searching for a better solution, found this jsvObjectGetChild(execInfo.hiddenRoot,"B­LE_SVC_D",0);
    Idea now is to move UUIDs and handles to this object to be more general. Value is already there.

    For testing of concept, this function was born. In my eyes it looks very complex.
    So I've 2 questions:

    • whats your feedback to hold uuids and handle in BLE_SVC_D
    • is there a better way to read value from BLE_SVC_D ?

    JsVar *jswrap_ESP32_get_Char_Value(JsVar *serviceUUID,JsVar *charUUID){
        JsVar *serviceKey; JsvObjectIterator serviceIt; JsVar *serviceData;
        JsVar *servicesData = jsvObjectGetChild(execInfo.hiddenRoot,"B­LE_SVC_D",0);
        JsVar *charKey; JsvObjectIterator charIt; JsVar *charData;
        JsVar *value;
        jsvObjectIteratorNew(&serviceIt,services­Data);
        while(jsvObjectIteratorHasValue(&service­It)){
            serviceKey = jsvObjectIteratorGetKey(&serviceIt);
            if(jsvIsEqual(serviceKey,serviceUUID)){
                serviceData = jsvObjectIteratorGetValue(&serviceIt);
                jsvObjectIteratorNew(&charIt,serviceData­);
                while(jsvObjectIteratorHasValue(&charIt)­){
                    charKey = jsvObjectIteratorGetKey(&charIt);
                    if(jsvIsEqual(charKey,charUUID)){
                        charData = jsvObjectIteratorGetValue(&charIt);
                        value = jsvObjectGetChild(charData,"value",0);
                        jsvUnLock(charData);
                    }
                    jsvUnLock(charKey);
                    jsvObjectIteratorNext(&charIt);
                }
                jsvObjectIteratorFree(&charIt);
                jsvUnLock(serviceData);
            }
            jsvUnLock(serviceKey);
            jsvObjectIteratorNext(&serviceIt);
        }
        jsvObjectIteratorFree(&serviceIt);
        jsvUnLock(servicesData);
        return value;
    }
    
  • So you intend to call jswrap_ESP32_get_Char_Value when the ESP32 asks Espruino for the data inside a characteristic?

    Rather than:

     jsvObjectIteratorNew(&serviceIt,services­Data);
        while(jsvObjectIteratorHasValue(&service­It)){
            serviceKey = jsvObjectIteratorGetKey(&serviceIt);
            if(jsvIsEqual(serviceKey,serviceUUID)){
             ...
    

    You could just use:

    serviceData = jsvSkipName(jsvFindChildFromVar(services­Data, serviceUUID, false));
    if (serviceData) {
    

    However one problem you have is UUIDs can be defined in different ways, eg 0xABCD, "ABCD", "AbCd" - so maybe what you're doing is better - but use bleVarToUUID and then compare the raw UUIDs, rather than using jsvIsEqual.

    you do say the ESP32 uses handles though? IMO it would probably be faster/easier to have a separate list of handle vs. value in execInfo.hiddenRoot instead? It might come in handy in other places - for instance I think notifications so something like that already?

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Handling of char value

Posted by Avatar for JumJum @JumJum

Actions