/*----------------------------------------------------------------------------* * NAME * startAdvertising * * DESCRIPTION * This function is called to start advertisements. * * Advertisement packet will contain Flags AD and Manufacturer-specific * AD with Manufacturer id set to CSR and payload set to the value of * the User Key 0. The payload size is set by the User Key 1. * * +--------+-------------------------------------------------+ * |FLAGS AD|MANUFACTURER AD | * +--------+-------------------------------------------------+ * 0 2 3 * * RETURNS * Nothing. * *---------------------------------------------------------------------------*/ static void BeaconInit(void) { uint8 filler; uint16 advInterval; uint8 advPayloadSize; ls_addr_type addressType = ls_addr_type_public; /* use public address */ /* initialise values from User CsKeys */ /* read User key 0 for the payload filler */ filler = (uint8)(CSReadUserKey(0) & 0x00FF); /* read User key 1 for the payload size */ advPayloadSize = (uint8)(CSReadUserKey(1) & 0x00FF); /* range check */ if((advPayloadSize < 1) || (advPayloadSize > MAX_ADVERT_PAYLOAD_SIZE)) { /* revert to default payload size */ advPayloadSize = DEFAULT_ADVERT_PAYLOAD_SIZE; } /* read User key 2 for the advertising interval */ advInterval = CSReadUserKey(2); /* range check */ if((advInterval < MIN_ADVERTISING_INTERVAL) || (advInterval > MAX_ADVERTISING_INTERVAL)) { /* revert to default advertising interval */ advInterval = DEFAULT_ADVERTISING_INTERVAL; } /* read address type from User key 3 */ if(CSReadUserKey(3)) { /* use random address type */ addressType = ls_addr_type_random; /* generate and set the random address */ appSetRandomAddress(); } /* set the GAP Broadcaster role */ GapSetMode(gap_role_broadcaster, gap_mode_discover_no, gap_mode_connect_no, gap_mode_bond_no, gap_mode_security_none); /* clear the existing advertisement data, if any */ LsStoreAdvScanData(0, NULL, ad_src_advertise); /* set the advertisement interval, API accepts the value in microseconds */ GapSetAdvInterval(advInterval * MILLISECOND, advInterval * MILLISECOND); }
/*----------------------------------------------------------------------------* * NAME * startAdvertising * * DESCRIPTION * This function is called to start advertisements. * * Advertisement packet will contain Flags AD and Manufacturer-specific * AD with Manufacturer id set to CSR and payload set to the value of * the User Key 0. The payload size is set by the User Key 1. * * +--------+-------------------------------------------------+ * |FLAGS AD|MANUFACTURER AD | * +--------+-------------------------------------------------+ * 0 2 3 * * RETURNS * Nothing. * *---------------------------------------------------------------------------*/ void startAdvertising(void) { uint8 advData[MAX_ADVERT_PACKET_SIZE]; uint16 offset = 0; uint8 filler; uint16 advInterval; uint8 advPayloadSize; ls_addr_type addressType = ls_addr_type_public; /* use public address */ /* initialise values from User CsKeys */ /* read User key 0 for the payload filler */ filler = (uint8)(CSReadUserKey(0) & 0x00FF); /* read User key 1 for the payload size */ advPayloadSize = (uint8)(CSReadUserKey(1) & 0x00FF); /* range check */ if((advPayloadSize < 1) || (advPayloadSize > MAX_ADVERT_PAYLOAD_SIZE)) { /* revert to default payload size */ advPayloadSize = DEFAULT_ADVERT_PAYLOAD_SIZE; } /* read User key 2 for the advertising interval */ advInterval = CSReadUserKey(2); /* range check */ if((advInterval < MIN_ADVERTISING_INTERVAL) || (advInterval > MAX_ADVERTISING_INTERVAL)) { /* revert to default advertising interval */ advInterval = DEFAULT_ADVERTISING_INTERVAL; } /* read address type from User key 3 */ if(CSReadUserKey(3)) { /* use random address type */ addressType = ls_addr_type_random; /* generate and set the random address */ appSetRandomAddress(); } /* set the GAP Broadcaster role */ GapSetMode(gap_role_broadcaster, gap_mode_discover_no, gap_mode_connect_no, gap_mode_bond_no, gap_mode_security_none); /* clear the existing advertisement data, if any */ LsStoreAdvScanData(0, NULL, ad_src_advertise); /* set the advertisement interval, API accepts the value in microseconds */ GapSetAdvInterval(advInterval * MILLISECOND, advInterval * MILLISECOND); /* manufacturer-specific data */ advData[0] = AD_TYPE_MANUF; /* CSR company code, little endian */ advData[1] = 0x0A; advData[2] = 0x00; /* fill in the rest of the advertisement */ for(offset = 0; offset < advPayloadSize; offset++) { advData[3 + offset] = filler; } /* store the advertisement data */ LsStoreAdvScanData(advPayloadSize + 3, advData, ad_src_advertise); /* Start broadcasting */ LsStartStopAdvertise(TRUE, whitelist_disabled, addressType); }
/*----------------------------------------------------------------------------* * NAME * initBeacon * * DESCRIPTION * This function initialises beacon data * * RETURNS * Nothing. * *---------------------------------------------------------------------------*/ void initBeacon(void) { /* read the config values from CsKeys */ uint16 beaconUuidMsw = CSReadUserKey(BEACON_UUID_MSW_USER_KEY_IDX); uint16 beaconMajor = CSReadUserKey(BEACON_MAJOR_USER_KEY_IDX); uint16 beaconMinor = CSReadUserKey(BEACON_MINOR_USER_KEY_IDX); uint16 txPower = CSReadUserKey(BEACON_TX_POWER_USER_KEY_IDX); /* set up beacon UUID */ if(beaconUuidMsw == BEACON_USER_KEY_DEFAULT_VALUE) { g_app_data.uuid[0] = BEACON_UUID_00; g_app_data.uuid[1] = BEACON_UUID_01; } else { g_app_data.uuid[0] = WORD_MSB(beaconUuidMsw); g_app_data.uuid[1] = WORD_LSB(beaconUuidMsw); } g_app_data.uuid[2] = BEACON_UUID_02; g_app_data.uuid[3] = BEACON_UUID_03; g_app_data.uuid[4] = BEACON_UUID_04; g_app_data.uuid[5] = BEACON_UUID_05; g_app_data.uuid[6] = BEACON_UUID_06; g_app_data.uuid[7] = BEACON_UUID_07; g_app_data.uuid[8] = BEACON_UUID_08; g_app_data.uuid[9] = BEACON_UUID_09; g_app_data.uuid[10] = BEACON_UUID_10; g_app_data.uuid[11] = BEACON_UUID_11; g_app_data.uuid[12] = BEACON_UUID_12; g_app_data.uuid[13] = BEACON_UUID_13; g_app_data.uuid[14] = BEACON_UUID_14; g_app_data.uuid[15] = BEACON_UUID_15; /* set up beacon major */ if(beaconMajor == BEACON_USER_KEY_DEFAULT_VALUE) { g_app_data.major = BEACON_DEFAULT_MAJOR; } else { g_app_data.major = beaconMajor; } /* set up beacon minor */ if(beaconMinor == BEACON_USER_KEY_DEFAULT_VALUE) { g_app_data.minor = BEACON_DEFAULT_MINOR; } else { g_app_data.minor = beaconMinor; } /* set up the TX power */ if(txPower == BEACON_USER_KEY_DEFAULT_VALUE) { g_app_data.txPower = BEACON_DEFAULT_TX_POWER; } else { g_app_data.txPower = txPower; } }