Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of OmniWheels by
virtual_rf_client.c
00001 /* 00002 * Copyright (c) 2016-2017, 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 #include "nsconfig.h" 00018 #include <stdio.h> 00019 #include <stdlib.h> 00020 #include <string.h> 00021 00022 #include "ns_types.h" 00023 #include "ns_trace.h" 00024 #include "platform/arm_hal_phy.h" 00025 #include "net_interface.h" 00026 #include "MAC/rf_driver_storage.h" 00027 #include "virtual_rf_defines.h" 00028 00029 00030 #define TRACE_GROUP "vrfc" 00031 00032 static phy_device_driver_s device_driver; 00033 static int8_t rf_driver_id = (-1); 00034 static const phy_rf_channel_configuration_s phy_2_4ghz = {2405000000, 5000000, 250000, 16, M_OQPSK}; 00035 static const phy_device_channel_page_s phy_channel_pages = { CHANNEL_PAGE_0, &phy_2_4ghz}; 00036 00037 static int8_t phy_rf_rx(const uint8_t *data_ptr, uint16_t data_len, uint8_t link_quality, int8_t dbm, int8_t driver_id) 00038 { 00039 if (rf_driver_id == -1 || rf_driver_id != driver_id || !device_driver.arm_net_virtual_tx_cb) { 00040 return -1; 00041 } 00042 00043 virtual_data_req_t data_req; 00044 uint8_t buf_temp[3]; 00045 uint8_t *ptr = buf_temp; 00046 memset(&data_req, 0, sizeof(virtual_data_req_t)); 00047 00048 *ptr++ = NAP_DATA_PHY_RAW_INDICATION; 00049 *ptr++ = link_quality; 00050 *ptr = dbm; 00051 00052 data_req.parameters = buf_temp; 00053 data_req.parameter_length = 3; 00054 data_req.msdu = data_ptr; 00055 data_req.msduLength = data_len; 00056 00057 return device_driver.arm_net_virtual_tx_cb(&data_req, driver_id); 00058 } 00059 00060 static uint8_t mac_mlme_status_2_phy_status(uint8_t status) 00061 { 00062 switch (status) { 00063 case MLME_TX_NO_ACK: 00064 return PHY_LINK_TX_FAIL; 00065 case MLME_BUSY_CHAN: 00066 return PHY_LINK_CCA_FAIL; 00067 case MLME_SUCCESS: 00068 return PHY_LINK_TX_DONE; 00069 default: 00070 return PHY_LINK_TX_SUCCESS; 00071 } 00072 } 00073 00074 static int8_t phy_rf_tx_done(int8_t driver_id, uint8_t tx_handle, phy_link_tx_status_e status, uint8_t cca_retry, uint8_t tx_retry) 00075 { 00076 if (rf_driver_id == -1 || rf_driver_id != driver_id || !device_driver.arm_net_virtual_tx_cb) { 00077 return -1; 00078 } 00079 (void) tx_handle; 00080 virtual_data_req_t data_req; 00081 memset(&data_req, 0, sizeof(virtual_data_req_t)); 00082 uint8_t buf_temp[4]; 00083 uint8_t *ptr = buf_temp; 00084 *ptr++ = NAP_DATA_PHY_RAW_RESPONSE; 00085 *ptr++ = mac_mlme_status_2_phy_status(status); 00086 *ptr++ = cca_retry; 00087 *ptr = tx_retry; 00088 data_req.msdu = buf_temp; 00089 data_req.msduLength = 4; 00090 00091 return device_driver.arm_net_virtual_tx_cb(&data_req, driver_id); 00092 } 00093 00094 static int8_t phy_rf_virtual_config_send(int8_t driver_id, const uint8_t *data, uint16_t length) 00095 { 00096 if (rf_driver_id != driver_id || !device_driver.arm_net_virtual_tx_cb) { 00097 return -1; 00098 } 00099 virtual_data_req_t data_req; 00100 memset(&data_req, 0, sizeof(virtual_data_req_t)); 00101 uint8_t buf_temp[length + 1]; 00102 uint8_t *ptr = buf_temp; 00103 *ptr++ = NAP_CONFIG_INTERNAL; 00104 memcpy(ptr, data, length); 00105 data_req.msdu = buf_temp; 00106 data_req.msduLength = length + 1; 00107 return device_driver.arm_net_virtual_tx_cb(&data_req, driver_id); 00108 } 00109 00110 static int8_t phy_rf_virtual_rx(const uint8_t *data_ptr, uint16_t data_len,int8_t driver_id) 00111 { 00112 if (rf_driver_id != driver_id || !data_ptr) { 00113 return -1; 00114 } 00115 00116 arm_device_driver_list_s *driver = arm_net_phy_driver_pointer(driver_id); 00117 if (!driver || !driver->phy_sap_identifier || !driver->phy_sap_upper_cb) { 00118 return -1; 00119 } 00120 00121 uint8_t data_type = *data_ptr++; 00122 arm_phy_sap_msg_t phy_msg; 00123 00124 switch (data_type) { 00125 case NAP_DATA_PHY_RAW_REQUEST: 00126 if (data_len <= 5) { 00127 return -1; 00128 } 00129 phy_msg.id = MACTUN_PD_SAP_NAP_IND; 00130 phy_msg.message.generic_data_ind.data_len = data_len - 1; 00131 phy_msg.message.generic_data_ind.data_ptr = data_ptr; 00132 phy_msg.message.generic_data_ind.dbm = 0; 00133 phy_msg.message.generic_data_ind.link_quality = 0; 00134 break; 00135 00136 case NAP_MLME_REQUEST: 00137 if (data_len < 3) { 00138 return -1; 00139 } 00140 phy_msg.id = MACTUN_MLME_NAP_EXTENSION; 00141 phy_msg.message.mlme_request.primitive = *data_ptr++; 00142 phy_msg.message.mlme_request.mlme_ptr = data_ptr; 00143 phy_msg.message.mlme_request.ptr_length = (data_len - 2); 00144 00145 break; 00146 00147 default: 00148 return -1; 00149 } 00150 00151 00152 return driver->phy_sap_upper_cb(driver->phy_sap_identifier, &phy_msg); 00153 } 00154 00155 int8_t virtual_rf_client_register(void) 00156 { 00157 if (rf_driver_id < 0) { 00158 memset(&device_driver, 0 , sizeof(phy_device_driver_s) ); 00159 device_driver.arm_net_virtual_rx_cb = &phy_rf_virtual_rx; 00160 device_driver.phy_rx_cb = &phy_rf_rx; 00161 device_driver.phy_tx_done_cb = &phy_rf_tx_done; 00162 device_driver.virtual_config_tx_cb = phy_rf_virtual_config_send; 00163 device_driver.driver_description = "VSND_Client"; 00164 device_driver.link_type = PHY_LINK_15_4_2_4GHZ_TYPE; 00165 device_driver.phy_channel_pages = &phy_channel_pages; 00166 rf_driver_id = arm_net_phy_register(&device_driver); 00167 00168 } 00169 return rf_driver_id; 00170 } 00171 00172
Generated on Fri Jul 22 2022 04:54:05 by
1.7.2
