Closed UrmilaKL closed 5 years ago
Please find the test case here, /* mbed Microcontroller Library
using namespace utest::v1;
void schedule_ble_events(BLE::OnEventsToProcessCallbackContext *context); const static char DEVICE_NAME[] = "MBEDOSHeartrate";
static events::EventQueue event_queue(/ event count / 16 * EVENTS_EVENT_SIZE);
class HeartrateDemo : ble::Gap::EventHandler { public: HeartrateDemo(BLE &ble, events::EventQueue &event_queue) : _ble(ble), _event_queue(event_queue), _led1(LED1, 1), _connected(false), _hr_uuid(GattService::UUID_HEART_RATE_SERVICE), _hr_counter(100), _hr_service(ble, _hr_counter, HeartRateService::LOCATION_FINGER), _adv_data_builder(_adv_buffer) { }
void start() {
printf("I am in start\n");
_ble.gap().setEventHandler(this);
printf("SetEventHandler called\n");
_ble.init(this, &HeartrateDemo::on_init_complete);
printf("Initialization Completed\n");
_event_queue.call_every(500, this, &HeartrateDemo::blink);
printf("Blink called\n");
_event_queue.call_every(1000, this, &HeartrateDemo::update_sensor_value);
printf("Updated sensor called\n");
_event_queue.dispatch_forever();
printf("dispatch called\n");
}
private: /* Callback triggered when the ble initialization process has finished / void on_init_complete(BLE::InitializationCompleteCallbackContext *params) { if (params->error != BLE_ERROR_NONE) { printf("Ble initialization failed."); return; }
// print_mac_address();
start_advertising();
}
void start_advertising() {
/* Create advertising parameters and payload */
ble::AdvertisingParameters adv_parameters(
ble::advertising_type_t::CONNECTABLE_UNDIRECTED,
ble::adv_interval_t(ble::millisecond_t(1000))
);
_adv_data_builder.setFlags();
_adv_data_builder.setAppearance(ble::adv_data_appearance_t::GENERIC_HEART_RATE_SENSOR);
_adv_data_builder.setLocalServiceList(mbed::make_Span(&_hr_uuid, 1));
_adv_data_builder.setName(DEVICE_NAME);
/* Setup advertising */
ble_error_t error = _ble.gap().setAdvertisingParameters(
ble::LEGACY_ADVERTISING_HANDLE,
adv_parameters
);
if (error) {
printf("_ble.gap().setAdvertisingParameters() failed\r\n");
return;
}
error = _ble.gap().setAdvertisingPayload(
ble::LEGACY_ADVERTISING_HANDLE,
_adv_data_builder.getAdvertisingData()
);
if (error) {
printf("_ble.gap().setAdvertisingPayload() failed\r\n");
return;
}
/* Start advertising */
error = _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE);
if (error) {
printf("_ble.gap().startAdvertising() failed\r\n");
return;
}
}
void update_sensor_value() {
if (_connected) {
// Do blocking calls or whatever is necessary for sensor polling.
// In our case, we simply update the HRM measurement.
_hr_counter++;
// 100 <= HRM bps <=175
if (_hr_counter == 175) {
_hr_counter = 100;
}
_hr_service.updateHeartRate(_hr_counter);
printf("\nHeartRate Measured value is %d \n ", _hr_counter);
}
}
void blink(void) {
_led1 = !_led1;
}
private: / Event handler /
void onDisconnectionComplete(const ble::DisconnectionCompleteEvent&) {
printf("Disocnnection compelted");
_ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE);
_connected = false;
}
virtual void onConnectionComplete(const ble::ConnectionCompleteEvent &event) {
if (event.getStatus() == BLE_ERROR_NONE) {
_connected = true;
printf("connection compelted");
}
}
private: BLE &_ble; events::EventQueue &_event_queue; DigitalOut _led1;
bool _connected;
UUID _hr_uuid;
uint8_t _hr_counter;
HeartRateService _hr_service;
uint8_t _adv_buffer[ble::LEGACY_ADVERTISING_MAX_SIZE];
ble::AdvertisingDataBuilder _adv_data_builder;
};
void display() { printf("My Example Application Initialized !\n"); BLE &ble = BLE::Instance(); printf("Instance created\n"); ble.onEventsToProcess(schedule_ble_events); printf("After events process\n"); HeartrateDemo demo(ble, event_queue); printf("After class creation\n"); demo.start(); printf("display function done\n"); TEST_ASSERT(true);
}
void schedule_ble_events(BLE::OnEventsToProcessCallbackContext *context) { event_queue.call(Callback<void()>(&context->ble, &BLE::processEvents)); }
utest::v1::status_t test_setup(const size_t number_of_cases) { // Setup Greentea using a reasonable timeout in seconds GREENTEA_SETUP(100, "default_auto"); return verbose_test_setup_handler(number_of_cases); }
Case cases[] = {
Case("Testing success test", display)
};
Specification specification(test_setup, cases);
int main() { printf("I am in main()"); return !Harness::run(specification); }
Internal Jira reference: https://jira.arm.com/browse/MBOCUSTRIA-1008
Is there any method so that we can pass time in dispatch_forever() function? like below dispatch(100);
cc @ARMmbed/mbed-os-pan
You can pass in the timeout for dispatch events, please check the documentation.
Take your code for example, dispatch(100)
means "Executes events until 100 milliseconds have passed".
@UrmilaKL I believe @desmond-blue has answered your question. Please close this issue if you have no further questions.
HI @Desmond-blue, If I add dispatch(100) to my code instead of dispatch_forever() test case is passing, But Like above if I try to add one more class with one more display1 test case, My test case is failing with "TIMEOUT" error, First test case will pass but second one will fail What may be the reason? Thanks Urmila Bhat
It appears Desmond has already answered your question.
mbed- cli version: 1.8.3 mbed os : 5.11.4
Description: [mbed os]: Greentea test case is failing with "timeout" error
Steps: 1. Fetch latest repo from the git server
Find the below main.cpp attached, Also I have attached the logs with -vv option
Note: I added the 'printf' statement in each of the function where it is getting stuck in "dispatch forever() method, If we remove that code of line test case is passing Let me know if anything else I need to add here
C:\mbed-examples\mbed-os>mbed test -m CY8CKIT_062_WIFI_BT -v -n tests-Group1-setADV_param -vv [mbed-7200] Working path "C:\mbed-examples\mbed-os" (program) [mbed-7200] Exec "c:\python27\python.exe -m pip list -l" in "C:\mbed-examples\mbed-os" Package Version
Total Static RAM memory (data + bss): 19244(+0) bytes Total Flash memory (text + data): 223895(+16) bytes
Image: BUILD/tests/CY8CKIT_062_WIFI_BT/GCC_ARM/TESTS/Group1/setADV_param/setADV_param.hex
Build successes:
mbedgt: test case results: 1 ERROR mbedgt: completed in 150.01 sec mbedgt: exited with code 1 [mbed-7200] ERROR: "mbedgt" returned error. Code: 1 Path: "C:\mbed-examples\mbed-os" Command: "mbedgt --test-spec .\BUILD\tests\CY8CKIT_062_WIFI_BT\GCC_ARM\test_spec.json -n tests-Group1-setADV_param -V" Tip: You could retry the last command with "-v" flag for verbose output