Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mac_fhss_callbacks.c Source File

mac_fhss_callbacks.c

00001 /*
00002  * Copyright (c) 2016-2018, Arm Limited and affiliates.
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #include <string.h>
00019 #include "nsconfig.h"
00020 #include "ns_types.h"
00021 #include "mac_api.h"
00022 #include "fhss_api.h "
00023 #include "MAC/IEEE802_15_4/mac_fhss_callbacks.h"
00024 #include "MAC/IEEE802_15_4/mac_defines.h"
00025 #include "MAC/IEEE802_15_4/sw_mac_internal.h"
00026 #include "MAC/IEEE802_15_4/mac_mlme.h"
00027 #include "MAC/IEEE802_15_4/mac_mcps_sap.h"
00028 #include "MAC/rf_driver_storage.h"
00029 #include "common_functions.h"
00030 
00031 
00032 uint16_t mac_read_tx_queue_sizes(const fhss_api_t *fhss_api, bool broadcast_queue)
00033 {
00034     protocol_interface_rf_mac_setup_s *mac_setup = get_sw_mac_ptr_by_fhss_api(fhss_api);
00035     if (!mac_setup) {
00036         return 0;
00037     }
00038     if (broadcast_queue == true) {
00039         return mac_setup->broadcast_queue_size;
00040     }
00041     return mac_setup->unicast_queue_size;
00042 }
00043 
00044 int mac_read_64bit_mac_address(const fhss_api_t *fhss_api, uint8_t *mac_address)
00045 {
00046     protocol_interface_rf_mac_setup_s *mac_setup = get_sw_mac_ptr_by_fhss_api(fhss_api);
00047     if (!mac_setup) {
00048         return -1;
00049     }
00050     memcpy(mac_address, mac_setup->mac64, 8);
00051     return 0;
00052 }
00053 
00054 uint32_t mac_read_phy_datarate(const fhss_api_t *fhss_api)
00055 {
00056     protocol_interface_rf_mac_setup_s *mac_setup = get_sw_mac_ptr_by_fhss_api(fhss_api);
00057     if (!mac_setup) {
00058         return 0;
00059     }
00060     uint32_t datarate = dev_get_phy_datarate(mac_setup->dev_driver->phy_driver, mac_setup->mac_channel_list.channel_page);
00061     // If datarate is not set, use default 250kbit/s.
00062     if (!datarate) {
00063         datarate = 250000;
00064     }
00065     return datarate;
00066 }
00067 
00068 uint32_t mac_read_phy_timestamp(const fhss_api_t *fhss_api)
00069 {
00070     protocol_interface_rf_mac_setup_s *mac_setup = get_sw_mac_ptr_by_fhss_api(fhss_api);
00071     if (!mac_setup) {
00072         return 0;
00073     }
00074     uint32_t timestamp;
00075     mac_setup->dev_driver->phy_driver->extension(PHY_EXTENSION_GET_TIMESTAMP, (uint8_t *)&timestamp);
00076     return timestamp;
00077 }
00078 
00079 int mac_set_channel(const fhss_api_t *fhss_api, uint8_t channel_number)
00080 {
00081     protocol_interface_rf_mac_setup_s *mac_setup = get_sw_mac_ptr_by_fhss_api(fhss_api);
00082     if (!mac_setup) {
00083         return -1;
00084     }
00085     if (mac_setup->mac_ack_tx_active || (mac_setup->active_pd_data_request && (mac_setup->active_pd_data_request->asynch_request || mac_setup->timer_mac_event == MAC_TIMER_ACK))) {
00086         return -1;
00087     }
00088     return mac_mlme_rf_channel_change(mac_setup, channel_number);
00089 }
00090 
00091 int mac_fhss_frame_tx(const fhss_api_t *fhss_api, int frame_type)
00092 {
00093     protocol_interface_rf_mac_setup_s *mac_setup = get_sw_mac_ptr_by_fhss_api(fhss_api);
00094     if (!mac_setup) {
00095         return -1;
00096     }
00097     if (FHSS_SYNCH_REQUEST_FRAME == frame_type) {
00098         if (mac_mlme_beacon_req_tx(mac_setup) != 1) {
00099             return -1;
00100         }
00101     } else if (FHSS_SYNCH_FRAME == frame_type) {
00102         if (mac_mlme_beacon_tx(mac_setup) != 0) {
00103             return -1;
00104         }
00105     } else {
00106         return -1;
00107     }
00108     return 0;
00109 }
00110 
00111 int mac_synch_lost(const fhss_api_t *fhss_api)
00112 {
00113     protocol_interface_rf_mac_setup_s *mac_setup = get_sw_mac_ptr_by_fhss_api(fhss_api);
00114     if (!mac_setup) {
00115         return -1;
00116     }
00117     mac_api_t *mac_api = get_sw_mac_api(mac_setup);
00118     if (!mac_api) {
00119         return -1;
00120     }
00121     mlme_sync_loss_t sync_loss;
00122     sync_loss.LossReason = BEACON_LOST;
00123     mac_api->mlme_ind_cb(mac_api, MLME_SYNC_LOSS, &sync_loss);
00124     return 0;
00125 }
00126 
00127 int mac_poll_tx_queue(const fhss_api_t *fhss_api)
00128 {
00129     protocol_interface_rf_mac_setup_s *mac_setup = get_sw_mac_ptr_by_fhss_api(fhss_api);
00130     if (!mac_setup) {
00131         return -1;
00132     }
00133     mcps_sap_trig_tx(mac_setup);
00134     return 0;
00135 }
00136 
00137 int mac_broadcast_notification(const fhss_api_t *fhss_api, uint32_t broadcast_time)
00138 {
00139     protocol_interface_rf_mac_setup_s *mac_setup = get_sw_mac_ptr_by_fhss_api(fhss_api);
00140     if (!mac_setup) {
00141         return -1;
00142     }
00143     if (mac_setup->tun_extension_rf_driver && mac_setup->tun_extension_rf_driver->phy_driver->virtual_config_tx_cb) {
00144         uint8_t data_buffer[5];
00145         data_buffer[0] = MAC_BROADCAST_EVENT;
00146         common_write_32_bit(broadcast_time, &data_buffer[1]);
00147         mac_setup->tun_extension_rf_driver->phy_driver->virtual_config_tx_cb(mac_setup->tun_extension_rf_driver->id, data_buffer, 5);
00148     }
00149     return 0;
00150 }
00151 
00152 int mac_get_coordinator_mac_address(const fhss_api_t *fhss_api, uint8_t *mac_address)
00153 {
00154     protocol_interface_rf_mac_setup_s *mac_setup = get_sw_mac_ptr_by_fhss_api(fhss_api);
00155     if (!mac_setup) {
00156         return -1;
00157     }
00158     memcpy(mac_address, mac_setup->coord_long_address, 8);
00159     return 0;
00160 }