Skip to content

Commit

Permalink
Add generic access, device information. Update some methods
Browse files Browse the repository at this point in the history
  • Loading branch information
GOB52 committed Aug 30, 2022
1 parent 5482264 commit df8858c
Show file tree
Hide file tree
Showing 5 changed files with 375 additions and 94 deletions.
146 changes: 118 additions & 28 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const char* ntpURL[] =
"time.cloudflare.com",
};

// For configrate time
// For configurate time
#ifdef M5S_WXBEACON2_GMT_OFFSET_HOUR
long gmtOffsetSec = 3600 * M5S_WXBEACON2_GMT_OFFSET_HOUR;
#else
Expand Down Expand Up @@ -114,11 +114,8 @@ struct ScopedWB2Client
*/
bool changeBeaconMode()
{
if((uint64_t)wb2address == 0)
{
WB2_LOGE("Address not yet obtained");
return false;
}
if(!wb2Client) { WB2_LOGE("Client null"); return false; }
if((uint64_t)wb2address == 0) { WB2_LOGE("Address not yet obtained"); return false; }

ScopedWB2Client wb2(*wb2Client);

Expand Down Expand Up @@ -151,11 +148,8 @@ bool changeBeaconMode()
*/
bool changeDefaultSetting()
{
if((uint64_t)wb2address == 0)
{
WB2_LOGE("Address not yet obtained");
return false;
}
if(!wb2Client) { WB2_LOGE("Client null"); return false; }
if((uint64_t)wb2address == 0) { WB2_LOGE("Address not yet obtained"); return false; }

ScopedWB2Client wb2(*wb2Client);

Expand Down Expand Up @@ -184,11 +178,8 @@ bool changeDefaultSetting()
/* turnon LED 1 second */
bool turnOnLED()
{
if((uint64_t)wb2address == 0)
{
WB2_LOGE("Address not yet obtained");
return false;
}
if(!wb2Client) { WB2_LOGE("Client null"); return false; }
if((uint64_t)wb2address == 0) { WB2_LOGE("Address not yet obtained"); return false; }

ScopedWB2Client wb2(*wb2Client);

Expand All @@ -212,8 +203,8 @@ time_t lastUpdate = -1;
void wb2_advertise_task(void*)
{
NimBLEScan* scan = BLEDevice::getScan();
WxBeacon2AdvertiseCallbacks* cb = new WxBeacon2AdvertiseCallbacks();
scan->setAdvertisedDeviceCallbacks(cb);
WxBeacon2AdvertiseCallbacks cb;
scan->setAdvertisedDeviceCallbacks(&cb);
scan->setInterval(1000);
scan->setWindow(900);
scan->setActiveScan(true);
Expand All @@ -222,22 +213,23 @@ void wb2_advertise_task(void*)
{
ulTaskNotifyTake( pdTRUE, portMAX_DELAY );

cb->clear();
cb.clear();

WB2_LOGI("Start scanning");
scan->start(60, false);
while(scan->isScanning()) { delay(100); }
WB2_LOGI("End of scanning. detected :%d", cb->detected());
WB2_LOGI("End of scanning. detected :%d", cb.detected());

if(cb->detected())
if(cb.detected())
{
wb2address = cb->address();
advertiseData = cb->data();
wb2address = cb.address();
advertiseData = cb.data();
lastUpdate = time(nullptr);
if(TURNON_LED) { turnOnLED(); }
}
updatedAdvertise = cb->detected();
updatedAdvertise = cb.detected();
advertiseBusy = false;
scan->clearResults();
}
}

Expand Down Expand Up @@ -314,6 +306,102 @@ void playAdvertiseData(const WxBeacon2::AdvertiseData& data)

constexpr char DEFAULT_TICKER_TEXT[] = "WEATHEROID Type A Airi";
constexpr char NOTICE_TICKER_TEXT[] = "Press and hold C to put the WxBeacon2 into the broadcast mode. ";

#if 0
bool test_connect()
{
if(!wb2Client) { WB2_LOGE("Client null"); return false; }
if((uint64_t)wb2address == 0) { WB2_LOGE("Address not yet obtained"); return false; }

ScopedWB2Client wb2(*wb2Client);

WB2_LOGI("connect to : %s", wb2address.toString().c_str());
if(!wb2.client().isConnected() && !wb2.client().connect(wb2address))
{
WB2_LOGE("Failed to connect");
return false;
}

std::string str;
auto b = wb2.client().getDeviceName(str);
printf("%d DeviceName:%s\n", b, str.c_str());

WxBeacon2::GenericAccesssService::Appearance app;
b = wb2.client().getAppearance(app);
printf("%d Appearnce:%x\n", b, app._category);

WxBeacon2::GenericAccesssService::PeripheralPreferredConnectionParameters params;
b = wb2.client().getPeripheralPreferredConnectionParameters(params);
printf("%d params:%x/%x/%x/%x\n", b,
params._intervalMin, params._intervalMax, params._slaveLatency, params._timeout);

b = wb2.client().getModelNumber(str);
printf("%d ModelNumber:[%s]\n", b, str.c_str());
b = wb2.client().getSerialNumber(str);
printf("%d SerialNumber:[%s]\n", b, str.c_str());
b = wb2.client().getFirmwareRevision(str);
printf("%d FirmRev:[%s]\n", b, str.c_str());
b = wb2.client().getHardwareRevision(str);
printf("%d HardRev:[%s]\n", b, str.c_str());
b = wb2.client().getManufacturerName(str);
printf("%d ManufacturerName:[%s]\n", b, str.c_str());

return true;
}

bool test_getLatestPageData()
{
if(advertiseData.format() == WxBeacon2::ADVFormat::E) { WB2_LOGE("Not recording mode"); return false; }

if(!wb2Client) { WB2_LOGE("Client null"); return false; }
if((uint64_t)wb2address == 0) { WB2_LOGE("Address not yet obtained"); return false; }

ScopedWB2Client wb2(*wb2Client);

WB2_LOGI("connect to : %s", wb2address.toString().c_str());
if(!wb2.client().isConnected() && !wb2.client().connect(wb2address))
{
WB2_LOGE("Failed to connect");
return false;
}

WxBeacon2::LatestPage lpage;
if(!wb2.client().getLatestPage(lpage)) { WB2_LOGE("Failed to get latest page"); return false; }
printf("Latest: %d,%d\n", lpage._page, lpage._row);

int retryCount = 3;
WxBeacon2::ResponseFlag flag = { 2 };
do
{
if(!wb2.client().requestPage(lpage._page, lpage._row)) { continue; } // request
for(;;) // get response flag
{
wb2.client().getResponseFlag(flag);
if((int)flag._updateFlag != 0) { break; }
delay(10);
}
}while((int)flag._updateFlag == 2 && retryCount--);
if((int)flag._updateFlag != 1) { WB2_LOGE("Failed to request or get response"); return false; }

int cnt = lpage._row + 1;
while(cnt--)
{
WxBeacon2::ResponseData data;
if(!wb2.client().getResponseData(data)) { WB2_LOGE("Failed to get response data"); return false; }
time_t t = flag._time32 + lpage._interval * data._data._row;
auto lt = std::localtime(&t);
printf("DATA: row:%02d time:%4d/%02d/%02d %02d:%02d:%02d\n",
data._data._row,
lt->tm_year + 1900,
lt->tm_mon + 1,
lt->tm_mday,
lt->tm_hour,
lt->tm_min,
lt->tm_sec);
}
return true;
}
#endif
//
}

Expand All @@ -336,7 +424,7 @@ void setup()
scfg.task_pinned_core = 0;
M5.Speaker.config(scfg);
M5.Speaker.begin();
M5.Speaker.setVolume(bd == m5::board_t::board_M5StackCore2 ? 64 : 128);
M5.Speaker.setVolume(bd == m5::board_t::board_M5Stack ? 128 : 64);
delay(500);

if (M5.Display.width() < M5.Display.height())
Expand Down Expand Up @@ -368,15 +456,15 @@ void setup()
abort();
}

// Configrate local time.
// Configurate local time.
M5.Display.setCursor(0,0);
M5.Display.printf("Configrate time");
M5.Display.printf("Configurate time");

configTime(gmtOffsetSec, daylightOffsetSec, ntpURL[0]);
std::tm t;
if(getLocalTime(&t))
{
WB2_LOGI("Configrate time : %4d/%02d/%02d (%d) %02d:%02d:%02d",
WB2_LOGI("Configurate time : %4d/%02d/%02d (%d) %02d:%02d:%02d",
t.tm_year + 1900,
t.tm_mon + 1,
t.tm_mday,
Expand Down Expand Up @@ -489,6 +577,8 @@ void loop()
{
if(!longPressB)
{
// test_connect();
// test_getLatestPageData();
#if 0
// TEST
auto lv = ticker->level() + 1;
Expand Down
13 changes: 12 additions & 1 deletion src/wxbeacon2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ bool WxBeacon2::MeasurementInterval::from(const std::string& v)
}

//
WxBeacon2::Data::Data(std::time_t pageTime, uint16_t interval, const WxBeacon2::ResponseData& rd)
WxBeacon2::Data::Data(const std::time_t pageTime, const uint16_t interval, const WxBeacon2::ResponseData& rd)
: _time(pageTime + rd._data._row * interval)
, _temperature(rd._data._temperature)
, _relativeHumidity(rd._data._relativeHumidity)
Expand Down Expand Up @@ -126,6 +126,17 @@ bool WxBeacon2::ADVSetting::from(const std::string& v)
return string2array(_array, v);
}

//
bool WxBeacon2::GenericAccesssService::Appearance::from(const std::string& v)
{
return string2array(_array, v);
}

bool WxBeacon2::GenericAccesssService::PeripheralPreferredConnectionParameters::from(const std::string& v)
{
return string2array(_array, v);
}

//
bool WxBeacon2::AdvertiseData::from(const WxBeacon2::BeaconMode bmode, const uint8_t* mdata, const size_t length)
{
Expand Down
Loading

0 comments on commit df8858c

Please sign in to comment.