Created
November 14, 2017 15:34
-
-
Save avtolstoy/45460e9cbd3f11de2faf626ad1b104d3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case CTRL_REQUEST_WIFI_SCAN: { | |
// Allocate buffer | |
system_ctrl_alloc_reply_data(req, 1024, nullptr); | |
pb_ostream_t stream = pb_ostream_from_buffer((uint8_t*)req->reply_data, req->reply_size); | |
// Message | |
particle_usb_WiFiScanResult res = {0}; | |
// Because aps is a repeated field, a custom encoding callback is required | |
{ | |
res.aps.funcs.encode = [](pb_ostream_t* stream, const pb_field_t* field, void* const* arg) -> bool { | |
// We need to pass all three fields, so create a temp state struct | |
struct state { | |
pb_ostream_t* stream; | |
const pb_field_t* field; | |
bool result; | |
}; | |
// Fill in | |
state s = {0}; | |
s.stream = stream; | |
s.field = field; | |
s.result = true; | |
// Scan | |
int ret = wlan_scan([](WiFiAccessPoint* sap, void* ptr) { | |
state* s = (state*)ptr; | |
if (s->result == false) { | |
// Some error has already occured | |
return; | |
} | |
particle_usb_WiFiAccessPoint ap = {0}; | |
// Fill in fields | |
strncpy(ap.ssid, sap->ssid, sizeof(sap->ssid)); | |
memcpy(ap.bssid, sap->bssid, sizeof(sap->bssid)); | |
ap.security = (particle_usb_WiFiSecurityType)sap->security; | |
ap.cipher = (particle_usb_WiFiSecurityCipher)sap->cipher; | |
ap.channel = sap->channel; | |
ap.maxDataRate = sap->maxDataRate; | |
ap.rssi = sap->rssi; | |
// Encode tag | |
if (!pb_encode_tag_for_field(s->stream, s->field)) { | |
s->result = false; | |
return; | |
} | |
// Encode submessage | |
if (!pb_encode_submessage(s->stream, particle_usb_WiFiAccessPoint_fields, &ap)) { | |
s->result = false; | |
return; | |
} | |
}, &s); | |
if (ret != 0) { | |
s.result = false; | |
} | |
return s.result; | |
}; | |
} | |
// Encode | |
bool r = pb_encode(&stream, particle_usb_WiFiScanResult_fields, &res); | |
// Set result | |
setResult(req, r == true ? SYSTEM_ERROR_NONE : SYSTEM_ERROR_UNKNOWN); | |
break; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment