├── Cfg └── Template │ ├── tftp-c_cfg.c │ └── tftp-c_cfg.h ├── LICENSE ├── NOTICE ├── Source ├── tftp-c.c ├── tftp-c.h └── tftp-c_type.h └── readme.md /Cfg/Template/tftp-c_cfg.c: -------------------------------------------------------------------------------- 1 | /* 2 | ********************************************************************************************************* 3 | * uC/TFTPc 4 | * Trivial File Transfer Protocol (client) 5 | * 6 | * Copyright 2004-2020 Silicon Laboratories Inc. www.silabs.com 7 | * 8 | * SPDX-License-Identifier: APACHE-2.0 9 | * 10 | * This software is subject to an open source license and is distributed by 11 | * Silicon Laboratories Inc. pursuant to the terms of the Apache License, 12 | * Version 2.0 available at www.apache.org/licenses/LICENSE-2.0. 13 | * 14 | ********************************************************************************************************* 15 | */ 16 | 17 | /* 18 | ********************************************************************************************************* 19 | * 20 | * TFTP CLIENT CONFIGURATION FILE 21 | * 22 | * TEMPLATE 23 | * 24 | * Filename : tftp-c_cfg.c 25 | * Version : V2.01.00 26 | ********************************************************************************************************* 27 | */ 28 | 29 | /* 30 | ********************************************************************************************************* 31 | ********************************************************************************************************* 32 | * INCLUDE FILES 33 | ********************************************************************************************************* 34 | ********************************************************************************************************* 35 | */ 36 | 37 | #include "tftp-c_cfg.h" 38 | 39 | 40 | /* 41 | ********************************************************************************************************* 42 | ********************************************************************************************************* 43 | * TFTP CLIENT CONFIGURATION STRUCTURE 44 | ********************************************************************************************************* 45 | ********************************************************************************************************* 46 | */ 47 | 48 | const TFTPc_CFG TFTPc_Cfg = { 49 | 50 | /* 51 | *-------------------------------------------------------------------------------------------------------- 52 | * TFTP SERVER CONFIGURATION 53 | *-------------------------------------------------------------------------------------------------------- 54 | */ 55 | /* TFTP Server hostname or IP address. */ 56 | "192.168.1.100", 57 | /* TFTP Server port number. */ 58 | 69, 59 | /* Select IP family of address when DNS resolution is used: */ 60 | NET_IP_ADDR_FAMILY_NONE, 61 | /* NET_IP_ADDR_FAMILY_NONE: No preference between IPv6 and IPv4.*/ 62 | /* NET_IP_ADDR_FAMILY_IPv4: Only IPv4 addr will be returned. */ 63 | /* NET_IP_ADDR_FAMILY_IPv6: Only IPv6 addr will be returned. */ 64 | 65 | /* 66 | *-------------------------------------------------------------------------------------------------------- 67 | * TIMEOUT CONFIGURATION 68 | *-------------------------------------------------------------------------------------------------------- 69 | */ 70 | 5000, /* Maximum inactivity time (ms) on RX. */ 71 | 5000 /* Maximum inactivity time (ms) on TX. */ 72 | }; 73 | 74 | -------------------------------------------------------------------------------- /Cfg/Template/tftp-c_cfg.h: -------------------------------------------------------------------------------- 1 | /* 2 | ********************************************************************************************************* 3 | * uC/TFTPc 4 | * Trivial File Transfer Protocol (client) 5 | * 6 | * Copyright 2004-2020 Silicon Laboratories Inc. www.silabs.com 7 | * 8 | * SPDX-License-Identifier: APACHE-2.0 9 | * 10 | * This software is subject to an open source license and is distributed by 11 | * Silicon Laboratories Inc. pursuant to the terms of the Apache License, 12 | * Version 2.0 available at www.apache.org/licenses/LICENSE-2.0. 13 | * 14 | ********************************************************************************************************* 15 | */ 16 | 17 | /* 18 | ********************************************************************************************************* 19 | * 20 | * TFTP CLIENT CONFIGURATION FILE 21 | * 22 | * TEMPLATE 23 | * 24 | * Filename : tftp-c_cfg.h 25 | * Version : V2.01.00 26 | ********************************************************************************************************* 27 | */ 28 | 29 | #ifndef TFTPc_CFG_MODULE_PRESENT 30 | #define TFTPc_CFG_MODULE_PRESENT 31 | 32 | #include 33 | 34 | 35 | /* 36 | ********************************************************************************************************* 37 | * TFTPc ARGUMENT CHECK CONFIGURATION 38 | * 39 | * Note(s) : (1) Configure TFTPc_CFG_ARG_CHK_EXT_EN to enable/disable the TFTP client external argument 40 | * check feature : 41 | * 42 | * (a) When ENABLED, ALL arguments received from any port interface provided by the developer 43 | * are checked/validated. 44 | * 45 | * (b) When DISABLED, NO arguments received from any port interface provided by the developer 46 | * are checked/validated. 47 | ********************************************************************************************************* 48 | */ 49 | /* Configure external argument check feature ... */ 50 | /* See Note 1. */ 51 | #define TFTPc_CFG_ARG_CHK_EXT_EN DEF_DISABLED 52 | /* DEF_DISABLED External argument check DISABLED */ 53 | /* DEF_ENABLED External argument check ENABLED */ 54 | 55 | /* 56 | ********************************************************************************************************* 57 | * TFTPc RUN-TIME STRUCTURE CONFIGURATION 58 | * 59 | * Note(s) : (1) This structure should be defined into a 'C' file. 60 | ********************************************************************************************************* 61 | */ 62 | 63 | extern const TFTPc_CFG TFTPc_Cfg; /* Must always be defined. */ 64 | 65 | 66 | /* 67 | ********************************************************************************************************* 68 | * TRACING 69 | ********************************************************************************************************* 70 | */ 71 | 72 | #ifndef TRACE_LEVEL_OFF 73 | #define TRACE_LEVEL_OFF 0 74 | #endif 75 | 76 | #ifndef TRACE_LEVEL_INFO 77 | #define TRACE_LEVEL_INFO 1 78 | #endif 79 | 80 | #ifndef TRACE_LEVEL_DBG 81 | #define TRACE_LEVEL_DBG 2 82 | #endif 83 | 84 | #define TFTPc_TRACE_LEVEL TRACE_LEVEL_DBG 85 | #define TFTPc_TRACE printf 86 | 87 | 88 | #endif 89 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | ATTENTION ALL USERS OF THIS REPOSITORY: 2 | 3 | The original work found in this repository is provided by Silicon Labs under the 4 | Apache License, Version 2.0. 5 | 6 | Any third party may contribute derivative works to the original work in which 7 | modifications are clearly identified as being licensed under: 8 | 9 | (1) the Apache License, Version 2.0 or a compatible open source license; or 10 | (2) under a proprietary license with a copy of such license deposited. 11 | 12 | All posted derivative works must clearly identify which license choice has been 13 | elected. 14 | 15 | No such posted derivative works will be considered to be a “Contribution” under 16 | the Apache License, Version 2.0. 17 | 18 | SILICON LABS MAKES NO WARRANTY WITH RESPECT TO ALL POSTED THIRD PARTY CONTENT 19 | AND DISCLAIMS ALL OTHER WARRANTIES OR LIABILITIES, INCLUDING ALL WARRANTIES OF 20 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE, OWNERSHIP, 21 | NON-INFRINGEMENT, AND NON-MISAPPROPRIATION. 22 | 23 | In the event a derivative work is desired to be submitted to Silicon Labs as a 24 | “Contribution” under the Apache License, Version 2.0, a “Contributor” must give 25 | written email notice to micrium@weston-embedded.com. Unless an email response in 26 | the affirmative to accept the derivative work as a “Contribution”, such email 27 | submission should be considered to have not been incorporated into the original 28 | work. 29 | -------------------------------------------------------------------------------- /Source/tftp-c.c: -------------------------------------------------------------------------------- 1 | /* 2 | ********************************************************************************************************* 3 | * uC/TFTPc 4 | * Trivial File Transfer Protocol (client) 5 | * 6 | * Copyright 2004-2020 Silicon Laboratories Inc. www.silabs.com 7 | * 8 | * SPDX-License-Identifier: APACHE-2.0 9 | * 10 | * This software is subject to an open source license and is distributed by 11 | * Silicon Laboratories Inc. pursuant to the terms of the Apache License, 12 | * Version 2.0 available at www.apache.org/licenses/LICENSE-2.0. 13 | * 14 | ********************************************************************************************************* 15 | */ 16 | 17 | /* 18 | ********************************************************************************************************* 19 | * 20 | * TFTP CLIENT 21 | * 22 | * Filename : tftp-c.c 23 | * Version : V2.01.00 24 | ********************************************************************************************************* 25 | * Note(s) : (1) Assumes the following versions (or more recent) of software modules are included in 26 | * the project build : 27 | * 28 | * (a) uC/CPU V1.29.02 29 | * (b) uC/LIB V1.38.00 30 | * (c) uC/Common V1.00.00 31 | * (d) uC/TCP-IP V3.02.00 32 | * 33 | * 34 | * (2) For additional details on the features available with uC/TFTPc, the API, the 35 | * installation, etc. Please refer to the uC/TFTPc documentation available at 36 | * https://doc.micrium.com. 37 | ********************************************************************************************************* 38 | */ 39 | 40 | /* 41 | ********************************************************************************************************* 42 | ********************************************************************************************************* 43 | * INCLUDE FILES 44 | ********************************************************************************************************* 45 | ********************************************************************************************************* 46 | */ 47 | 48 | #define MICRIUM_SOURCE 49 | #define TFTPc_MODULE 50 | #include "tftp-c.h" 51 | #include 52 | #include 53 | #include 54 | 55 | /* 56 | ********************************************************************************************************* 57 | ********************************************************************************************************* 58 | * LOCAL DEFINES 59 | ********************************************************************************************************* 60 | ********************************************************************************************************* 61 | */ 62 | 63 | /* 64 | ********************************************************************************************************* 65 | * FILE OPEN MODE DEFINES 66 | ********************************************************************************************************* 67 | */ 68 | 69 | #define TFTPc_FILE_OPEN_RD 0 70 | #define TFTPc_FILE_OPEN_WR 1 71 | 72 | 73 | /* 74 | ********************************************************************************************************* 75 | * TFTP OPCODE DEFINES 76 | ********************************************************************************************************* 77 | */ 78 | 79 | #define TFTP_OPCODE_RRQ 1 80 | #define TFTP_OPCODE_WRQ 2 81 | #define TFTP_OPCODE_DATA 3 82 | #define TFTP_OPCODE_ACK 4 83 | #define TFTP_OPCODE_ERR 5 84 | 85 | 86 | /* 87 | ********************************************************************************************************* 88 | * TFTP PKT OFFSET DEFINES 89 | ********************************************************************************************************* 90 | */ 91 | 92 | #define TFTP_PKT_OFFSET_OPCODE 0 93 | #define TFTP_PKT_OFFSET_FILENAME 2 94 | #define TFTP_PKT_OFFSET_BLK_NBR 2 95 | #define TFTP_PKT_OFFSET_ERR_CODE 2 96 | #define TFTP_PKT_OFFSET_ERR_MSG 4 97 | #define TFTP_PKT_OFFSET_DATA 4 98 | 99 | 100 | /* 101 | ********************************************************************************************************* 102 | * TFTP PKT FIELD SIZE DEFINES 103 | ********************************************************************************************************* 104 | */ 105 | 106 | #define TFTP_PKT_SIZE_OPCODE 2 107 | #define TFTP_PKT_SIZE_BLK_NBR 2 108 | #define TFTP_PKT_SIZE_ERR_CODE 2 109 | #define TFTP_PKT_SIZE_NULL 1 110 | 111 | 112 | /* 113 | ********************************************************************************************************* 114 | * TFTP TRANSFER MODE DEFINES 115 | ********************************************************************************************************* 116 | */ 117 | 118 | #define TFTP_MODE_NETASCII_STR "netascii" 119 | #define TFTP_MODE_NETASCII_STR_LEN 8 120 | #define TFTP_MODE_BINARY_STR "octet" 121 | #define TFTP_MODE_BINARY_STR_LEN 5 122 | 123 | 124 | /* 125 | ********************************************************************************************************* 126 | * TFTP PKT DEFINES 127 | ********************************************************************************************************* 128 | */ 129 | 130 | #define TFTPc_DATA_BLOCK_SIZE 512 131 | #define TFTPc_PKT_BUF_SIZE (TFTPc_DATA_BLOCK_SIZE + TFTP_PKT_SIZE_OPCODE + TFTP_PKT_SIZE_BLK_NBR) 132 | 133 | #define TFTPc_MAX_NBR_TX_RETRY 3 134 | 135 | 136 | /* 137 | ********************************************************************************************************* 138 | * TFTP ERROR CODES 139 | ********************************************************************************************************* 140 | */ 141 | 142 | #define TFTP_ERR_CODE_NOT_DEF 0 /* Not defined, see err message (if any). */ 143 | #define TFTP_ERR_CODE_FILE_NOT_FOUND 1 /* File not found. */ 144 | #define TFTP_ERR_CODE_ACCESS_VIOLATION 2 /* Access violation. */ 145 | #define TFTP_ERR_CODE_DISK_FULL 3 /* Disk full of allocation exceeded. */ 146 | #define TFTP_ERR_CODE_ILLEGAL_OP 4 /* Illegal TFTP operation. */ 147 | #define TFTP_ERR_CODE_UNKNOWN_ID 5 /* Unknown transfer ID. */ 148 | #define TFTP_ERR_CODE_FILE_EXISTS 6 /* File already exists. */ 149 | #define TFTP_ERR_CODE_NO_USER 7 /* No such user. */ 150 | 151 | 152 | /* 153 | ********************************************************************************************************* 154 | * TFTPc ERROR MESSAGES 155 | ********************************************************************************************************* 156 | */ 157 | 158 | #define TFTPc_ERR_MSG_WR_ERR "File write error" 159 | #define TFTPc_ERR_MSG_RD_ERR "File read error" 160 | 161 | 162 | /* 163 | ********************************************************************************************************* 164 | * TFTPc CLIENT STATE DEFINES 165 | ********************************************************************************************************* 166 | */ 167 | 168 | #define TFTPc_STATE_DATA_GET 1 169 | #define TFTPc_STATE_DATA_PUT 2 170 | #define TFTPc_STATE_DATA_PUT_WAIT_LAST_ACK 3 171 | #define TFTPc_STATE_TRANSFER_COMPLETE 4 172 | 173 | 174 | /* 175 | ********************************************************************************************************* 176 | ********************************************************************************************************* 177 | * LOCAL DATA TYPES 178 | ********************************************************************************************************* 179 | ********************************************************************************************************* 180 | */ 181 | 182 | /* 183 | ********************************************************************************************************* 184 | * FILE ACCESS MODE DATA TYPE 185 | ********************************************************************************************************* 186 | */ 187 | 188 | typedef CPU_INT08U TFTPc_FILE_ACCESS; 189 | 190 | 191 | /* 192 | ********************************************************************************************************* 193 | * TFTPc BLOCK NUMBER DATA TYPE 194 | ********************************************************************************************************* 195 | */ 196 | 197 | typedef CPU_INT16U TFTPc_BLK_NBR; 198 | 199 | 200 | /* 201 | ********************************************************************************************************* 202 | * TFTPc SERVER OBJECT DATA TYPE 203 | ********************************************************************************************************* 204 | */ 205 | 206 | typedef struct tftpc_server_obj { 207 | CPU_CHAR *HostnamePtr; 208 | NET_PORT_NBR PortNbr; 209 | NET_IP_ADDR_FAMILY AddrFamily; 210 | } TFTPc_SERVER_OBJ; 211 | 212 | 213 | /* 214 | ********************************************************************************************************* 215 | ********************************************************************************************************* 216 | * LOCAL GLOBAL VARIABLES 217 | ********************************************************************************************************* 218 | ********************************************************************************************************* 219 | */ 220 | 221 | static KAL_LOCK_HANDLE TFTPc_LockHandle; /* Lock Handle. */ 222 | 223 | static TFTPc_CFG *TFTPc_DfltCfgPtr; /* Default TFTP Configuration Pointer. */ 224 | 225 | static NET_IP_ADDR_FAMILY TFTPc_ServerAddrFamily; /* IP address family of TFTP server. */ 226 | 227 | static CPU_INT16U TFTPc_RxBlkNbrNext; /* Next rx'd blk nbr expected. */ 228 | 229 | static CPU_INT08U TFTPc_RxPktBuf[TFTPc_PKT_BUF_SIZE];/* Last rx'd pkt buf. */ 230 | static CPU_INT32S TFTPc_RxPktLen; /* Last rx'd pkt len. */ 231 | static CPU_INT16U TFTPc_RxPktOpcode; /* Last rx'd pkt opcode. */ 232 | 233 | static CPU_INT16U TFTPc_TxPktBlkNbr; /* Last tx'd pkt blk nbr. */ 234 | static CPU_INT08U TFTPc_TxPktBuf[TFTPc_PKT_BUF_SIZE];/* Last tx'd pkt buf. */ 235 | static CPU_INT16U TFTPc_TxPktLen; /* Last tx'd pkt len. */ 236 | static CPU_INT08U TFTPc_TxPktRetry; /* Nbr of time last tx'd pkt had been sent. */ 237 | 238 | static NET_SOCK_ID TFTPc_SockID; /* Client sock id. */ 239 | static NET_SOCK_ADDR TFTPc_SockAddr; /* Server sock addr IP. */ 240 | 241 | static CPU_BOOLEAN TFTPc_TID_Set; /* Indicates whether the terminal ID is set or not. */ 242 | 243 | static CPU_INT08U TFTPc_State; /* Cur state of TFTPc state machine. */ 244 | 245 | static void *TFTPc_FileHandle; /* Handle to cur opened file. */ 246 | 247 | 248 | /* 249 | ********************************************************************************************************* 250 | ********************************************************************************************************* 251 | * LOCAL FUNCTION PROTOTYPES 252 | ********************************************************************************************************* 253 | ********************************************************************************************************* 254 | */ 255 | 256 | /* -------------------- LOCK FNCT --------------------- */ 257 | static void TFTPc_LockAcquire ( TFTPc_ERR *p_err); 258 | 259 | static void TFTPc_LockRelease (void); 260 | 261 | /* -------------------- INIT FNCT --------------------- */ 262 | static void TFTPc_InitSession (void); 263 | 264 | static CPU_BOOLEAN TFTPc_SockInit ( CPU_CHAR *p_server_hostname, 265 | NET_PORT_NBR server_port, 266 | NET_IP_ADDR_FAMILY ip_family, 267 | TFTPc_ERR *p_err); 268 | 269 | 270 | /* ----------------- PROCESSING FNCTS ----------------- */ 271 | static void TFTPc_Processing (const TFTPc_CFG *p_cfg, 272 | TFTPc_ERR *p_err); 273 | 274 | static void TFTPc_StateDataGet ( TFTPc_ERR *p_err); 275 | 276 | static void TFTPc_StateDataPut ( TFTPc_ERR *p_err); 277 | 278 | static CPU_INT16U TFTPc_GetRxBlkNbr (void); 279 | 280 | 281 | /* ---------------- FILE ACCESS FNCTS ----------------- */ 282 | static void *TFTPc_FileOpenMode ( CPU_CHAR *p_filename, 283 | TFTPc_FILE_ACCESS file_access); 284 | 285 | static CPU_INT16U TFTPc_DataWr ( TFTPc_ERR *p_err); 286 | 287 | static CPU_INT16U TFTPc_DataRd ( TFTPc_ERR *p_err); 288 | 289 | 290 | /* --------------------- RX FNCTS --------------------- */ 291 | static NET_SOCK_RTN_CODE TFTPc_RxPkt ( NET_SOCK_ID sock_id, 292 | void *p_pkt, 293 | CPU_INT16U pkt_len, 294 | TFTPc_ERR *p_err); 295 | 296 | 297 | /* --------------------- TX FNCTS --------------------- */ 298 | static void TFTPc_TxReq ( CPU_INT16U req_opcode, 299 | CPU_CHAR *p_filename, 300 | TFTPc_MODE mode, 301 | TFTPc_ERR *p_err); 302 | 303 | static void TFTPc_TxData ( TFTPc_BLK_NBR blk_nbr, 304 | CPU_INT16U data_len, 305 | TFTPc_ERR *p_err); 306 | 307 | static void TFTPc_TxAck ( TFTPc_BLK_NBR blk_nbr, 308 | TFTPc_ERR *p_err); 309 | 310 | static void TFTPc_TxErr ( CPU_INT16U err_code, 311 | CPU_CHAR *p_err_msg, 312 | TFTPc_ERR *p_err); 313 | 314 | static NET_SOCK_RTN_CODE TFTPc_TxPkt ( NET_SOCK_ID sock_id, 315 | void *p_pkt, 316 | CPU_INT16U pkt_len, 317 | NET_SOCK_ADDR *p_addr_remote, 318 | NET_SOCK_ADDR_LEN addr_len, 319 | TFTPc_ERR *p_err); 320 | 321 | 322 | static void TFTPc_Terminate (void); 323 | 324 | 325 | /* 326 | ********************************************************************************************************* 327 | * TFTPc_Init() 328 | * 329 | * Description : (1) Initialize the TFTPc suite. 330 | * 331 | * (a) Create TFTPc Lock. 332 | * (b) Save pointer to TFTPc Configuration. 333 | * 334 | * 335 | * Argument(s) : p_cfg Pointer to TFTPc Configuration to use as default. 336 | * 337 | * p_err Pointer to variable that will receive the return error code from this function : 338 | * 339 | * TFTPc_ERR_NONE Initialization was successful. 340 | * TFTPc_ERR_NULL_PTR Null pointer was passed as argument. 341 | * TFTPc_ERR_MEM_ALLOC Memory error while creating lock. 342 | * TFTPc_ERR_FAULT_INIT TFTPc Initialization faulted. 343 | * 344 | * ------------ RETURNED BY TFTPc_SetDfltCfg() ------------ 345 | * See TFTPc_SetDfltCfg() for additional return error codes. 346 | * 347 | * Return(s) : DEF_OK, if initialization was successful. 348 | * DEF_FAIL, otherwise. 349 | * 350 | * Caller(s) : Application. 351 | * 352 | * Note(s) : None. 353 | ********************************************************************************************************* 354 | */ 355 | 356 | CPU_BOOLEAN TFTPc_Init (const TFTPc_CFG *p_cfg, 357 | TFTPc_ERR *p_err) 358 | { 359 | CPU_BOOLEAN result; 360 | KAL_ERR err_kal; 361 | 362 | 363 | #if (TFTPc_CFG_ARG_CHK_EXT_EN == DEF_ENABELD) 364 | if (p_err == DEF_NULL) { 365 | CPU_SW_EXCEPTION(;); 366 | } 367 | 368 | if (p_cfg == DEF_NULL) { 369 | *p_err = TFTPc_ERR_NULL_PTR; 370 | goto exit; 371 | } 372 | #endif 373 | 374 | /* ---------------- CREATE TFTPC LOCK ----------------- */ 375 | TFTPc_LockHandle = KAL_LockCreate("TFTPc Lock", 376 | KAL_OPT_CREATE_NONE, 377 | &err_kal); 378 | switch (err_kal) { 379 | case KAL_ERR_NONE: 380 | break; 381 | 382 | case KAL_ERR_MEM_ALLOC: 383 | result = DEF_FAIL; 384 | *p_err = TFTPc_ERR_MEM_ALLOC; 385 | goto exit; 386 | 387 | default: 388 | result = DEF_FAIL; 389 | *p_err = TFTPc_ERR_FAULT_INIT; 390 | goto exit; 391 | } 392 | 393 | /* ------------ SET DEFAULT CONFIGURATION ------------- */ 394 | (void)TFTPc_SetDfltCfg(p_cfg, p_err); 395 | if (*p_err != TFTPc_ERR_NONE) { 396 | result = DEF_FAIL; 397 | goto exit; 398 | } 399 | 400 | result = DEF_OK; 401 | *p_err = TFTPc_ERR_NONE; 402 | 403 | 404 | exit: 405 | return (result); 406 | } 407 | 408 | 409 | /* 410 | ********************************************************************************************************* 411 | * TFTPc_SetDfltCfg() 412 | * 413 | * Description : Set default TFTPc Configuration. 414 | * 415 | * Argument(s) : p_cfg Pointer to TFTPc Configuration to set as default. 416 | * 417 | * p_err Pointer to variable that will receive the return error code from this function : 418 | * 419 | * TFTPc_ERR_NONE Configuration set up was successful. 420 | * TFTPc_ERR_NULL_PTR Null pointer was passed as argument. 421 | * 422 | * ------------ RETURNED BY TFTPc_LockAcquire() ------------ 423 | * See TFTPc_LockAcquire() for additional return error codes. 424 | * 425 | * Return(s) : DEF_OK, if configuration was set up successfully. 426 | * DEF_FAIl, otherwise. 427 | * 428 | * Caller(s) : Application, 429 | * TFTPc_Init(). 430 | * 431 | * Note(s) : None. 432 | ********************************************************************************************************* 433 | */ 434 | 435 | CPU_BOOLEAN TFTPc_SetDfltCfg (const TFTPc_CFG *p_cfg, 436 | TFTPc_ERR *p_err) 437 | { 438 | CPU_BOOLEAN result; 439 | 440 | 441 | #if (TFTPc_CFG_ARG_CHK_EXT_EN == DEF_ENABELD) 442 | if (p_err == DEF_NULL) { 443 | CPU_SW_EXCEPTION(;); 444 | } 445 | 446 | if (p_cfg == DEF_NULL) { 447 | *p_err = TFTPc_ERR_NULL_PTR; 448 | goto exit; 449 | } 450 | #endif 451 | 452 | TFTPc_LockAcquire(p_err); 453 | if (*p_err != TFTPc_ERR_NONE) { 454 | result = DEF_FAIL; 455 | goto exit; 456 | } 457 | 458 | TFTPc_DfltCfgPtr = (TFTPc_CFG *)p_cfg; 459 | 460 | TFTPc_ServerAddrFamily = p_cfg->ServerAddrFamily; 461 | 462 | result = DEF_OK; 463 | *p_err = TFTPc_ERR_NONE; 464 | 465 | TFTPc_LockRelease(); 466 | 467 | 468 | exit: 469 | return (result); 470 | } 471 | 472 | 473 | /* 474 | ********************************************************************************************************* 475 | * TFTPc_Get() 476 | * 477 | * Description : Get a file from the TFTP server. 478 | * 479 | * Argument(s) : p_cfg Pointer to TFTPc Configuration to use. 480 | * 481 | * DEF_NULL, if default configuration must be used. 482 | * 483 | * p_filename_local Pointer to name of the file to be written by the client. 484 | * 485 | * p_filename_remote Pointer to name of the file to be read from the server. 486 | * 487 | * mode TFTP transfer mode : 488 | * 489 | * TFTPc_MODE_NETASCII ASCII mode. 490 | * TFTPc_MODE_OCTET Binary mode. 491 | * 492 | * p_err Pointer to variable that will receive the return error code from this function : 493 | * 494 | * TFTPc_ERR_NONE TFTP operation was successful. 495 | * TFTPC_ERR_FILE_OPEN File opening failed. 496 | * TFTPc_ERR_TX Transmission of TFTP request faulted. 497 | * 498 | * ------------ RETURNED BY TFTPc_LockAcquire() ------------ 499 | * See TFTPc_LockAcquire() for additional return error codes. 500 | * 501 | * ------------ RETURNED BY TFTPc_SockInit() ------------ 502 | * See TFTPc_SockInit() for additional return error codes. 503 | * 504 | * ------------ RETURNED BY TFTPc_Processing() ------------ 505 | * See TFTPc_Processing() for additional return error codes. 506 | * 507 | * Return(s) : DEF_OK, if file was get from server successfully. 508 | * DEF_FAIL, otherwise. 509 | * 510 | * Caller(s) : Application. 511 | * 512 | * This function is a TFTP client application interface (API) function & MAY be called by 513 | * application function(s). 514 | * 515 | * Note(s) : None. 516 | ********************************************************************************************************* 517 | */ 518 | 519 | CPU_BOOLEAN TFTPc_Get (const TFTPc_CFG *p_cfg, 520 | CPU_CHAR *p_filename_local, 521 | CPU_CHAR *p_filename_remote, 522 | TFTPc_MODE mode, 523 | TFTPc_ERR *p_err) 524 | { 525 | TFTPc_CFG *p_cfg_to_use; 526 | CPU_CHAR *p_server_hostname; 527 | NET_PORT_NBR server_port; 528 | NET_IP_ADDR_FAMILY ip_family; 529 | NET_IP_ADDR_FAMILY ip_family_tmp; 530 | CPU_BOOLEAN result; 531 | CPU_BOOLEAN is_hostname; 532 | CPU_BOOLEAN retry; 533 | 534 | 535 | #if (TFTPc_CFG_ARG_CHK_EXT_EN == DEF_ENABELD) 536 | if (p_err == DEF_NULL) { 537 | CPU_SW_EXCEPTION(;); 538 | } 539 | 540 | if (p_filename_local == DEF_NULL) { 541 | *p_err = TFTPc_ERR_NULL_PTR; 542 | goto exit; 543 | } 544 | 545 | if (p_filename_remote == DEF_NULL) { 546 | *p_err = TFTPc_ERR_NULL_PTR; 547 | goto exit; 548 | } 549 | #endif 550 | 551 | TFTPc_LockAcquire(p_err); 552 | if (*p_err != TFTPc_ERR_NONE) { 553 | result = DEF_FAIL; 554 | goto exit; 555 | } 556 | 557 | TFTPc_TRACE_INFO(("TFTPc_Get: Request for %s\n\r", p_filename_remote)); 558 | 559 | TFTPc_InitSession(); 560 | 561 | if (p_cfg == DEF_NULL) { 562 | p_cfg_to_use = TFTPc_DfltCfgPtr; 563 | p_server_hostname = TFTPc_DfltCfgPtr->ServerHostnamePtr; 564 | server_port = TFTPc_DfltCfgPtr->ServerPortNbr; 565 | ip_family = TFTPc_ServerAddrFamily; 566 | } else { 567 | p_cfg_to_use = (TFTPc_CFG *)p_cfg; 568 | p_server_hostname = p_cfg->ServerHostnamePtr; 569 | server_port = p_cfg->ServerPortNbr; 570 | ip_family = p_cfg->ServerAddrFamily; 571 | } 572 | 573 | if (ip_family == NET_IP_ADDR_FAMILY_NONE) { 574 | ip_family_tmp = NET_IP_ADDR_FAMILY_IPv6; 575 | } else { 576 | ip_family_tmp = ip_family; 577 | } 578 | 579 | /* Open file */ 580 | TFTPc_FileHandle = TFTPc_FileOpenMode(p_filename_local, TFTPc_FILE_OPEN_WR); 581 | if (TFTPc_FileHandle == (void *)0) { 582 | TFTPc_Terminate(); 583 | result = DEF_FAIL; 584 | *p_err = TFTPC_ERR_FILE_OPEN; 585 | goto exit_release; 586 | } 587 | 588 | retry = DEF_YES; 589 | while (retry == DEF_YES) { 590 | is_hostname = TFTPc_SockInit(p_server_hostname, /* Init sock. */ 591 | server_port, 592 | ip_family_tmp, 593 | p_err); 594 | if (*p_err != TFTPc_ERR_NONE) { 595 | if ((ip_family == NET_IP_ADDR_FAMILY_NONE) && 596 | (ip_family_tmp == NET_IP_ADDR_FAMILY_IPv6) && 597 | (is_hostname == DEF_YES) ) { 598 | retry = DEF_YES; 599 | ip_family_tmp = NET_IP_ADDR_FAMILY_IPv4; 600 | } else { 601 | TFTPc_Terminate(); 602 | retry = DEF_NO; 603 | result = DEF_FAIL; 604 | goto exit_release; 605 | } 606 | } else { 607 | retry = DEF_NO; 608 | } 609 | 610 | if (retry == DEF_NO) { 611 | /* Tx rd req. */ 612 | TFTPc_TxReq(TFTP_OPCODE_RRQ, p_filename_remote, mode, p_err); 613 | if (*p_err != TFTPc_ERR_NONE) { 614 | if ((ip_family == NET_IP_ADDR_FAMILY_NONE) && 615 | (ip_family_tmp == NET_IP_ADDR_FAMILY_IPv6) && 616 | (is_hostname == DEF_YES) ) { 617 | retry = DEF_YES; 618 | ip_family_tmp = NET_IP_ADDR_FAMILY_IPv4; 619 | } else { 620 | TFTPc_Terminate(); 621 | retry = DEF_NO; 622 | result = DEF_FAIL; 623 | *p_err = TFTPc_ERR_TX; 624 | goto exit_release; 625 | } 626 | } else { 627 | retry = DEF_NO; 628 | } 629 | 630 | if (retry == DEF_NO) { 631 | /* Process req. */ 632 | TFTPc_RxBlkNbrNext = 1; 633 | TFTPc_State = TFTPc_STATE_DATA_GET; 634 | 635 | TFTPc_Processing(p_cfg_to_use, p_err); 636 | if (*p_err != TFTPc_ERR_NONE) { 637 | result = DEF_FAIL; 638 | goto exit_release; 639 | } 640 | 641 | TFTPc_ServerAddrFamily = ip_family_tmp; 642 | } 643 | 644 | } 645 | } 646 | 647 | result = DEF_OK; 648 | *p_err = TFTPc_ERR_NONE; 649 | 650 | 651 | exit_release: 652 | TFTPc_LockRelease(); 653 | 654 | exit: 655 | return (result); 656 | } 657 | 658 | 659 | /* 660 | ********************************************************************************************************* 661 | * TFTPc_Put() 662 | * 663 | * Description : Put a file on the TFTP server. 664 | * 665 | * Argument(s) : p_cfg Pointer to TFTPc Configuration to use. 666 | * 667 | * DEF_NULL, if default configuration must be used. 668 | * 669 | * p_filename_local Pointer to name of the file to be read by the client. 670 | * 671 | * p_filename_remote Pointer to name of the file to be written to the server. 672 | * 673 | * mode TFTP transfer mode : 674 | * 675 | * TFTPc_MODE_NETASCII ASCII mode. 676 | * TFTPc_MODE_OCTET Binary mode. 677 | * 678 | * p_err Pointer to variable that will receive the return error code from this function : 679 | * 680 | * TFTPc_ERR_NONE TFTP operation was successful. 681 | * TFTPC_ERR_FILE_OPEN File opening failed. 682 | * TFTPc_ERR_TX Transmission of TFTP request faulted. 683 | * 684 | * ------------ RETURNED BY TFTPc_LockAcquire() ------------ 685 | * See TFTPc_LockAcquire() for additional return error codes. 686 | * 687 | * ------------ RETURNED BY TFTPc_SockInit() ------------ 688 | * See TFTPc_SockInit() for additional return error codes. 689 | * 690 | * ------------ RETURNED BY TFTPc_Processing() ------------ 691 | * See TFTPc_Processing() for additional return error codes. 692 | * 693 | * Return(s) : DEF_OK, if file was put on server successfully. 694 | * DEF_FAIL, otherwise. 695 | * 696 | * Caller(s) : Application. 697 | * 698 | * This function is a TFTP client application interface (API) function & MAY be called by 699 | * application function(s). 700 | * 701 | * Note(s) : None. 702 | ********************************************************************************************************* 703 | */ 704 | 705 | CPU_BOOLEAN TFTPc_Put (const TFTPc_CFG *p_cfg, 706 | CPU_CHAR *p_filename_local, 707 | CPU_CHAR *p_filename_remote, 708 | TFTPc_MODE mode, 709 | TFTPc_ERR *p_err) 710 | { 711 | TFTPc_CFG *p_cfg_to_use; 712 | CPU_CHAR *p_server_hostname; 713 | NET_PORT_NBR server_port; 714 | NET_IP_ADDR_FAMILY ip_family; 715 | NET_IP_ADDR_FAMILY ip_family_tmp; 716 | CPU_BOOLEAN result; 717 | CPU_BOOLEAN is_hostname; 718 | CPU_BOOLEAN retry; 719 | 720 | 721 | #if (TFTPc_CFG_ARG_CHK_EXT_EN == DEF_ENABELD) 722 | if (p_err == DEF_NULL) { 723 | CPU_SW_EXCEPTION(;); 724 | } 725 | 726 | if (p_filename_local == DEF_NULL) { 727 | *p_err = TFTPc_ERR_NULL_PTR; 728 | goto exit; 729 | } 730 | 731 | if (p_filename_remote == DEF_NULL) { 732 | *p_err = TFTPc_ERR_NULL_PTR; 733 | goto exit; 734 | } 735 | #endif 736 | 737 | TFTPc_LockAcquire(p_err); 738 | if (*p_err != TFTPc_ERR_NONE) { 739 | result = DEF_FAIL; 740 | goto exit; 741 | } 742 | 743 | TFTPc_TRACE_INFO(("TFTPc_Put: Request for %s\n\r", p_filename_local)); 744 | 745 | TFTPc_InitSession(); 746 | 747 | if (p_cfg == DEF_NULL) { 748 | p_cfg_to_use = TFTPc_DfltCfgPtr; 749 | p_server_hostname = TFTPc_DfltCfgPtr->ServerHostnamePtr; 750 | server_port = TFTPc_DfltCfgPtr->ServerPortNbr; 751 | ip_family = TFTPc_ServerAddrFamily; 752 | } else { 753 | p_cfg_to_use = (TFTPc_CFG *)p_cfg; 754 | p_server_hostname = p_cfg->ServerHostnamePtr; 755 | server_port = p_cfg->ServerPortNbr; 756 | ip_family = p_cfg->ServerAddrFamily; 757 | } 758 | 759 | if (ip_family == NET_IP_ADDR_FAMILY_NONE) { 760 | ip_family_tmp = NET_IP_ADDR_FAMILY_IPv6; 761 | } else { 762 | ip_family_tmp = ip_family; 763 | } 764 | 765 | /* Open file. */ 766 | TFTPc_FileHandle = TFTPc_FileOpenMode(p_filename_local, TFTPc_FILE_OPEN_RD); 767 | if (TFTPc_FileHandle == (void *)0) { 768 | result = DEF_FAIL; 769 | *p_err = TFTPC_ERR_FILE_OPEN; 770 | goto exit_release; 771 | } 772 | 773 | retry = DEF_YES; 774 | while (retry == DEF_YES) { 775 | 776 | is_hostname = TFTPc_SockInit(p_server_hostname, /* Init sock. */ 777 | server_port, 778 | ip_family_tmp, 779 | p_err); 780 | if (*p_err != TFTPc_ERR_NONE) { 781 | if ((ip_family == NET_IP_ADDR_FAMILY_NONE) && 782 | (ip_family_tmp == NET_IP_ADDR_FAMILY_IPv6) && 783 | (is_hostname == DEF_YES) ) { 784 | retry = DEF_YES; 785 | ip_family_tmp = NET_IP_ADDR_FAMILY_IPv4; 786 | 787 | } else { 788 | TFTPc_Terminate(); 789 | retry = DEF_NO; 790 | result = DEF_FAIL; 791 | goto exit_release; 792 | } 793 | } else { 794 | retry = DEF_NO; 795 | } 796 | 797 | if (retry == DEF_NO) { 798 | /* Tx rd to server. */ 799 | TFTPc_TxReq(TFTP_OPCODE_WRQ, p_filename_remote, mode, p_err); 800 | if (*p_err != TFTPc_ERR_NONE) { 801 | if ((ip_family == NET_IP_ADDR_FAMILY_NONE) && 802 | (ip_family_tmp == NET_IP_ADDR_FAMILY_IPv6) && 803 | (is_hostname == DEF_YES) ) { 804 | retry = DEF_YES; 805 | ip_family_tmp = NET_IP_ADDR_FAMILY_IPv4; 806 | } else { 807 | TFTPc_Terminate(); 808 | retry = DEF_NO; 809 | result = DEF_FAIL; 810 | *p_err = TFTPc_ERR_TX; 811 | goto exit_release; 812 | } 813 | } else { 814 | retry = DEF_NO; 815 | } 816 | 817 | if (retry == DEF_NO) { 818 | /* Process req. */ 819 | TFTPc_TxPktBlkNbr = 0; 820 | TFTPc_State = TFTPc_STATE_DATA_PUT; 821 | 822 | TFTPc_Processing(p_cfg_to_use, p_err); 823 | if (*p_err != TFTPc_ERR_NONE) { 824 | result = DEF_FAIL; 825 | goto exit_release; 826 | } 827 | 828 | TFTPc_ServerAddrFamily = ip_family_tmp; 829 | } 830 | } 831 | } 832 | 833 | result = DEF_OK; 834 | *p_err = TFTPc_ERR_NONE; 835 | 836 | 837 | exit_release: 838 | TFTPc_LockRelease(); 839 | 840 | exit: 841 | return (result); 842 | } 843 | 844 | 845 | /* 846 | ********************************************************************************************************* 847 | ********************************************************************************************************* 848 | * LOCAL FUNCTIONS 849 | ********************************************************************************************************* 850 | ********************************************************************************************************* 851 | */ 852 | 853 | /* 854 | ********************************************************************************************************* 855 | * TFTPc_LockAcquire() 856 | * 857 | * Description : $$$$ Add function description. 858 | * 859 | * Argument(s) : p_err $$$$ Add description for 'p_err' 860 | * 861 | * Return(s) : $$$$ Add return value description. 862 | * 863 | * Caller(s) : none. 864 | * 865 | * Note(s) : none. 866 | ********************************************************************************************************* 867 | */ 868 | 869 | static void TFTPc_LockAcquire (TFTPc_ERR *p_err) 870 | { 871 | KAL_ERR err; 872 | 873 | 874 | KAL_LockAcquire(TFTPc_LockHandle, KAL_OPT_PEND_NONE, 0, &err); 875 | if (err != KAL_ERR_NONE) { 876 | *p_err = TFTPc_ERR_LOCK; 877 | goto exit; 878 | } 879 | 880 | *p_err = TFTPc_ERR_NONE; 881 | 882 | 883 | exit: 884 | return; 885 | } 886 | 887 | 888 | /* 889 | ********************************************************************************************************* 890 | * TFTPc_LockRelease() 891 | * 892 | * Description : $$$$ Add function description. 893 | * 894 | * Argument(s) : none. 895 | * 896 | * Return(s) : $$$$ Add return value description. 897 | * 898 | * Caller(s) : none. 899 | * 900 | * Note(s) : none. 901 | ********************************************************************************************************* 902 | */ 903 | 904 | static void TFTPc_LockRelease (void) 905 | { 906 | KAL_ERR err; 907 | 908 | 909 | KAL_LockRelease(TFTPc_LockHandle, &err); 910 | } 911 | 912 | 913 | /* 914 | ********************************************************************************************************* 915 | * TFTPc_InitSession() 916 | * 917 | * Description : Initialize the TFTP session. 918 | * 919 | * Argument(s) : none. 920 | * 921 | * Return(s) : none. 922 | * 923 | * Caller(s) : TFTPc_Get(), 924 | * TFTPc_Put(). 925 | * 926 | * Note(s) : none. 927 | ********************************************************************************************************* 928 | */ 929 | 930 | static void TFTPc_InitSession (void) 931 | { 932 | TFTPc_SockID = NET_SOCK_ID_NONE; 933 | TFTPc_FileHandle = (void *)0; 934 | 935 | TFTPc_RxPktLen = 0; 936 | TFTPc_TxPktLen = 0; 937 | 938 | TFTPc_TxPktRetry = 0; 939 | 940 | TFTPc_TID_Set = DEF_NO; 941 | } 942 | 943 | 944 | /* 945 | ********************************************************************************************************* 946 | * TFTPc_SockInit() 947 | * 948 | * Description : Initialize the communication socket. 949 | * 950 | * Argument(s) : p_server_hostname Pointer to hostname or IP address string of the TFTP server. 951 | * 952 | * server_port Port number of the TFTP server. 953 | * 954 | * Return(s) : Error message: 955 | * 956 | * TFTPc_ERR_NONE No error. 957 | * TFTPc_ERR_NO_SOCK Could not open socket. 958 | * 959 | * Caller(s) : TFTPc_Get(), 960 | * TFTPc_Put(). 961 | * 962 | * Note(s) : none. 963 | ********************************************************************************************************* 964 | */ 965 | 966 | static CPU_BOOLEAN TFTPc_SockInit (CPU_CHAR *p_server_hostname, 967 | NET_PORT_NBR server_port, 968 | NET_IP_ADDR_FAMILY ip_family, 969 | TFTPc_ERR *p_err) 970 | { 971 | NET_SOCK_ID *p_sock_id; 972 | NET_SOCK_ADDR *p_server_sock_addr; 973 | CPU_BOOLEAN is_hostname; 974 | NET_ERR err; 975 | 976 | 977 | p_sock_id = &TFTPc_SockID; 978 | p_server_sock_addr = &TFTPc_SockAddr; 979 | 980 | (void)NetApp_ClientDatagramOpenByHostname(p_sock_id, 981 | p_server_hostname, 982 | server_port, 983 | ip_family, 984 | p_server_sock_addr, 985 | &is_hostname, 986 | &err); 987 | if (err != NET_APP_ERR_NONE) { 988 | *p_err = TFTPc_ERR_INVALID_PROTO_FAMILY; 989 | goto exit; 990 | } 991 | 992 | (void)NetSock_CfgBlock(*p_sock_id, NET_SOCK_BLOCK_SEL_BLOCK, &err); 993 | 994 | *p_err = TFTPc_ERR_NONE; 995 | 996 | 997 | exit: 998 | return (is_hostname); 999 | } 1000 | 1001 | 1002 | /* 1003 | ********************************************************************************************************* 1004 | * TFTPc_Processing() 1005 | * 1006 | * Description : Process data transfer. 1007 | * 1008 | * Argument(s) : p_cfg Pointer to TFTPc configuration object. 1009 | * 1010 | * p_err Pointer to variable that will receive the return error code from this function : 1011 | * 1012 | * TFTPc_ERR_NONE Data transfer successful. 1013 | * 1014 | * ------- RETURNED BY TFTPc_RxPkt() : ------- 1015 | * TFTPc_ERR_RX_TIMEOUT Receive timeout. 1016 | * TFTPc_ERR_RX Error receiving packet. 1017 | * 1018 | * ----- RETURNED BY TFTPc_StateDataGet() : ----- 1019 | * TFTPc_ERR_ERR_PKT_RX Error packet received. 1020 | * TFTPc_ERR_INVALID_OPCODE_RX Invalid opcode received. 1021 | * TFTPc_ERR_FILE_WR Error writing to file. 1022 | * TFTPc_ERR_TX Error transmitting packet. 1023 | * 1024 | * ----- RETURNED BY TFTPc_StateDataPut() : ----- 1025 | * TFTPc_ERR_ERR_PKT_RX Error packet received. 1026 | * TFTPc_ERR_INVALID_OPCODE_RX Invalid opcode received. 1027 | * TFTPc_ERR_INVALID_STATE Invalid state machine state. 1028 | * TFTPc_ERR_FILE_RD Error reading file. 1029 | * TFTPc_ERR_TX Error transmitting packet. 1030 | * 1031 | * ----- RETURNED BY TFTPc_TxPkt() : ----- 1032 | * TFTPc_ERR_TX Error transmitting packet. 1033 | * 1034 | * Return(s) : none. 1035 | * 1036 | * Caller(s) : TFTPc_Get(), 1037 | * TFTPc_Put(). 1038 | * 1039 | * Note(s) : none. 1040 | ********************************************************************************************************* 1041 | */ 1042 | 1043 | static void TFTPc_Processing (const TFTPc_CFG *p_cfg, 1044 | TFTPc_ERR *p_err) 1045 | { 1046 | NET_SOCK_RTN_CODE rx_pkt_len; 1047 | NET_SOCK_ADDR_LEN sock_addr_size; 1048 | CPU_INT32U timeout; 1049 | NET_ERR err_net; 1050 | 1051 | 1052 | /* Set rx sock timeout. */ 1053 | timeout = p_cfg->RxInactivityTimeout_ms; 1054 | NetSock_CfgTimeoutRxQ_Set(TFTPc_SockID, 1055 | timeout, 1056 | &err_net); 1057 | 1058 | 1059 | 1060 | while (TFTPc_State != TFTPc_STATE_TRANSFER_COMPLETE) { 1061 | 1062 | rx_pkt_len = TFTPc_RxPkt((NET_SOCK_ID ) TFTPc_SockID, 1063 | (void *)&TFTPc_RxPktBuf[0], 1064 | (CPU_INT16U ) sizeof(TFTPc_RxPktBuf), 1065 | (TFTPc_ERR *) p_err); 1066 | switch (*p_err) { 1067 | case TFTPc_ERR_NONE: 1068 | TFTPc_RxPktLen = rx_pkt_len; 1069 | TFTPc_RxPktOpcode = NET_UTIL_VAL_GET_NET_16(&TFTPc_RxPktBuf[TFTP_PKT_OFFSET_OPCODE]); 1070 | 1071 | switch (TFTPc_State) { 1072 | case TFTPc_STATE_DATA_GET: 1073 | TFTPc_StateDataGet(p_err); 1074 | break; 1075 | 1076 | 1077 | case TFTPc_STATE_DATA_PUT: 1078 | case TFTPc_STATE_DATA_PUT_WAIT_LAST_ACK: 1079 | TFTPc_StateDataPut(p_err); 1080 | break; 1081 | 1082 | 1083 | default: 1084 | *p_err = TFTPc_ERR_INVALID_STATE; 1085 | break; 1086 | } 1087 | 1088 | break; 1089 | 1090 | 1091 | case TFTPc_ERR_RX_TIMEOUT: 1092 | if (TFTPc_TxPktLen > 0) { /* If pkt tx'd ... */ 1093 | /* ... and max retry NOT reached, ... */ 1094 | if (TFTPc_TxPktRetry < TFTPc_MAX_NBR_TX_RETRY) { 1095 | /* ... re-tx last tx'd pkt. */ 1096 | sock_addr_size = sizeof(NET_SOCK_ADDR); 1097 | (void)TFTPc_TxPkt((NET_SOCK_ID ) TFTPc_SockID, 1098 | (void *)&TFTPc_TxPktBuf[0], 1099 | (CPU_INT16U ) TFTPc_TxPktLen, 1100 | (NET_SOCK_ADDR *)&TFTPc_SockAddr, 1101 | (NET_SOCK_ADDR_LEN) sock_addr_size, 1102 | (TFTPc_ERR *) p_err); 1103 | 1104 | TFTPc_TxPktRetry++; 1105 | } 1106 | } 1107 | break; 1108 | 1109 | 1110 | case TFTPc_ERR_RX: 1111 | default: 1112 | break; 1113 | } 1114 | 1115 | if (*p_err != TFTPc_ERR_NONE) { 1116 | TFTPc_TRACE_INFO(("TFTPc_Processing: Error, session terminated\n\r")); 1117 | TFTPc_State = TFTPc_STATE_TRANSFER_COMPLETE; 1118 | } 1119 | } 1120 | 1121 | TFTPc_Terminate(); 1122 | } 1123 | 1124 | 1125 | /* 1126 | ********************************************************************************************************* 1127 | * TFTPc_StateDataGet() 1128 | * 1129 | * Description : Process received packets for a read request. 1130 | * 1131 | * Argument(s) : p_err Pointer to variable that will receive the return error code from this function : 1132 | * 1133 | * TFTPc_ERR_NONE No error. 1134 | * TFTPc_ERR_ERR_PKT_RX Error packet received. 1135 | * TFTPc_ERR_INVALID_OPCODE_RX Invalid opcode received. 1136 | * 1137 | * ------- RETURNED BY TFTPc_DataWr() : ------- 1138 | * TFTPc_ERR_FILE_WR Error writing to file. 1139 | * 1140 | * ------- RETURNED BY TFTPc_TxAck() : ------- 1141 | * TFTPc_ERR_TX Error transmitting packet. 1142 | * Return(s) : none. 1143 | * 1144 | * Caller(s) : TFTPc_Processing(). 1145 | * 1146 | * Note(s) : (1) If the data block received is not the expected one, nothing is written in the file, 1147 | * and the function silently returns. 1148 | ********************************************************************************************************* 1149 | */ 1150 | 1151 | static void TFTPc_StateDataGet (TFTPc_ERR *p_err) 1152 | { 1153 | CPU_INT16U rx_blk_nbr; 1154 | CPU_INT16U wr_data_len; 1155 | TFTPc_ERR err; 1156 | 1157 | 1158 | switch (TFTPc_RxPktOpcode) { 1159 | case TFTP_OPCODE_DATA: 1160 | TFTPc_TRACE_INFO(("TFTPc_StateDataGet: Opcode DATA rx'd\n\r")); 1161 | *p_err = TFTPc_ERR_NONE; 1162 | break; 1163 | 1164 | 1165 | case TFTP_OPCODE_ERR: 1166 | TFTPc_TRACE_INFO(("TFTPc_StateDataGet: Opcode ERROR rx'd\n\r")); 1167 | *p_err = TFTPc_ERR_ERR_PKT_RX; 1168 | break; 1169 | 1170 | 1171 | case TFTP_OPCODE_ACK: 1172 | case TFTP_OPCODE_WRQ: 1173 | case TFTP_OPCODE_RRQ: 1174 | default: 1175 | TFTPc_TRACE_INFO(("TFTPc_StateDataGet: Invalid opcode rx'd\n\r")); 1176 | TFTPc_TxErr((CPU_INT16U ) TFTP_ERR_CODE_ILLEGAL_OP, 1177 | (CPU_CHAR *) 0, 1178 | (TFTPc_ERR *)&err); 1179 | *p_err = TFTPc_ERR_INVALID_OPCODE_RX; 1180 | break; 1181 | } 1182 | 1183 | if (*p_err != TFTPc_ERR_NONE) { 1184 | return; 1185 | } 1186 | 1187 | 1188 | rx_blk_nbr = TFTPc_GetRxBlkNbr(); /* Get rx'd pkt's blk nbr. */ 1189 | 1190 | if (rx_blk_nbr == TFTPc_RxBlkNbrNext) { /* If data blk nbr expected, (see Note #1) ... */ 1191 | wr_data_len = TFTPc_DataWr(p_err); /* ... wr data to file ... */ 1192 | 1193 | if (*p_err == TFTPc_ERR_NONE) { 1194 | TFTPc_TxAck(rx_blk_nbr, &err); /* ... and tx ack. */ 1195 | TFTPc_TxPktRetry = 0; 1196 | 1197 | if (wr_data_len < TFTPc_DATA_BLOCK_SIZE) { /* If rx'd data len < TFTP blk size, ... */ 1198 | TFTPc_State = TFTPc_STATE_TRANSFER_COMPLETE; /* ... last blk rx'd. */ 1199 | 1200 | } else { 1201 | TFTPc_RxBlkNbrNext++; 1202 | } 1203 | 1204 | } else { /* Err wr'ing data to file. */ 1205 | TFTPc_TxErr((CPU_INT16U ) TFTP_ERR_CODE_NOT_DEF, 1206 | (CPU_CHAR *) TFTPc_ERR_MSG_WR_ERR, 1207 | (TFTPc_ERR *)&err); 1208 | } 1209 | } 1210 | } 1211 | 1212 | 1213 | /* 1214 | ********************************************************************************************************* 1215 | * TFTPc_StateDataPut() 1216 | * 1217 | * Description : Process received packet for a write request. 1218 | * 1219 | * Argument(s) : p_err Pointer to variable that will receive the return error code from this function : 1220 | * 1221 | * TFTPc_ERR_NONE No error. 1222 | * TFTPc_ERR_ERR_PKT_RX Error packet received. 1223 | * TFTPc_ERR_INVALID_OPCODE_RX Invalid opcode received. 1224 | * TFTPc_ERR_INVALID_STATE Invalid state machine state. 1225 | * 1226 | * ------- RETURNED BY TFTPc_DataRd() : ------- 1227 | * TFTPc_ERR_FILE_RD Error reading file. 1228 | * 1229 | * ------- RETURNED BY TFTPc_TxData() : ------- 1230 | * TFTPc_ERR_TX Error transmitting packet. 1231 | * 1232 | * Return(s) : none. 1233 | * 1234 | * Caller(s) : TFTPc_Processing(). 1235 | * 1236 | * Note(s) : (1) If the acknowledge block received is not the expected one, nothing is done, and the 1237 | * function silently returns. This is done in order to prevent the 'Sorcerer's Apprentice' 1238 | * bug. Only a receive timeout is supposed to trigger a block retransmission. 1239 | ********************************************************************************************************* 1240 | */ 1241 | 1242 | static void TFTPc_StateDataPut (TFTPc_ERR *p_err) 1243 | { 1244 | CPU_INT16U rx_blk_nbr; 1245 | CPU_INT16U rd_data_len; 1246 | TFTPc_ERR err; 1247 | 1248 | 1249 | switch (TFTPc_RxPktOpcode) { 1250 | case TFTP_OPCODE_ACK: 1251 | TFTPc_TRACE_INFO(("TFTPc_StateDataGet: Opcode ACK rx'd\n\r")); 1252 | *p_err = TFTPc_ERR_NONE; 1253 | break; 1254 | 1255 | 1256 | case TFTP_OPCODE_ERR: 1257 | TFTPc_TRACE_INFO(("TFTPc_StateDataPut: Opcode ERROR rx'd\n\r")); 1258 | *p_err = TFTPc_ERR_ERR_PKT_RX; 1259 | break; 1260 | 1261 | 1262 | case TFTP_OPCODE_DATA: 1263 | case TFTP_OPCODE_WRQ: 1264 | case TFTP_OPCODE_RRQ: 1265 | default: 1266 | TFTPc_TRACE_INFO(("TFTPc_StateDataPut: Invalid opcode rx'd\n\r")); 1267 | TFTPc_TxErr((CPU_INT16U ) TFTP_ERR_CODE_ILLEGAL_OP, 1268 | (CPU_CHAR *) 0, 1269 | (TFTPc_ERR *)&err); 1270 | *p_err = TFTPc_ERR_INVALID_OPCODE_RX; 1271 | break; 1272 | } 1273 | 1274 | if (*p_err != TFTPc_ERR_NONE) { 1275 | return; 1276 | } 1277 | 1278 | 1279 | 1280 | rx_blk_nbr = TFTPc_GetRxBlkNbr(); /* Get rx'd pkt's blk nbr. */ 1281 | 1282 | if (rx_blk_nbr == TFTPc_TxPktBlkNbr) { /* If ACK blk nbr matches data sent (see Note #1) ... */ 1283 | 1284 | switch (TFTPc_State) { 1285 | case TFTPc_STATE_DATA_PUT: /* ... and more data to read, ... */ 1286 | rd_data_len = TFTPc_DataRd(p_err); /* ... rd next blk from file ... */ 1287 | 1288 | if (*p_err == TFTPc_ERR_NONE) { 1289 | TFTPc_TxPktBlkNbr++; /* ... and tx data. */ 1290 | TFTPc_TxData(TFTPc_TxPktBlkNbr, rd_data_len, p_err); 1291 | TFTPc_TxPktRetry = 0; 1292 | 1293 | if (rd_data_len < TFTPc_DATA_BLOCK_SIZE) { 1294 | TFTPc_State = TFTPc_STATE_DATA_PUT_WAIT_LAST_ACK; 1295 | } 1296 | 1297 | } else { /* Err rd'ing data to file. */ 1298 | TFTPc_TxErr((CPU_INT16U ) TFTP_ERR_CODE_NOT_DEF, 1299 | (CPU_CHAR *) TFTPc_ERR_MSG_RD_ERR, 1300 | (TFTPc_ERR *)&err); 1301 | } 1302 | break; 1303 | 1304 | 1305 | case TFTPc_STATE_DATA_PUT_WAIT_LAST_ACK: /* If waiting for last ack, ... */ 1306 | TFTPc_State = TFTPc_STATE_TRANSFER_COMPLETE; /* ... transfer completed. */ 1307 | break; 1308 | 1309 | 1310 | default: 1311 | *p_err = TFTPc_ERR_INVALID_STATE; 1312 | break; 1313 | } 1314 | } 1315 | } 1316 | 1317 | 1318 | /* 1319 | ********************************************************************************************************* 1320 | * TFTPc_GetRxBlkNbr() 1321 | * 1322 | * Description : Extract the block number from the received TFTP packet. 1323 | * 1324 | * Argument(s) : none. 1325 | * 1326 | * Return(s) : Received block number. 1327 | * 1328 | * Caller(s) : TFTPc_StateDataPut(). 1329 | * 1330 | * Note(s) : none. 1331 | ********************************************************************************************************* 1332 | */ 1333 | 1334 | static CPU_INT16U TFTPc_GetRxBlkNbr (void) 1335 | { 1336 | CPU_INT16U blk_nbr; 1337 | 1338 | 1339 | blk_nbr = NET_UTIL_VAL_GET_NET_16(&TFTPc_RxPktBuf[TFTP_PKT_OFFSET_BLK_NBR]); 1340 | 1341 | return (blk_nbr); 1342 | } 1343 | 1344 | 1345 | /* 1346 | ********************************************************************************************************* 1347 | * TFTPc_FileOpenMode() 1348 | * 1349 | * Description : Open the specified file for read or write. 1350 | * 1351 | * Argument(s) : filename Name of the file to open. 1352 | * 1353 | * file_access File access : 1354 | * 1355 | * TFTPc_FILE_OPEN_RD Open for reading. 1356 | * TFTPc_FILE_OPEN_WR Open for writing. 1357 | * 1358 | * Return(s) : Pointer to a file handle for the opened file, if NO error. 1359 | * 1360 | * Pointer to NULL, otherwise. 1361 | * 1362 | * Caller(s) : TFTPc_Get(), 1363 | * TFTPc_Put(). 1364 | * 1365 | * Note(s) : none. 1366 | ********************************************************************************************************* 1367 | */ 1368 | 1369 | static void *TFTPc_FileOpenMode (CPU_CHAR *p_filename, 1370 | TFTPc_FILE_ACCESS file_access) 1371 | { 1372 | void *pfile; 1373 | 1374 | 1375 | pfile = (void *)0; 1376 | switch (file_access) { 1377 | case TFTPc_FILE_OPEN_RD: 1378 | pfile = NetFS_FileOpen(p_filename, 1379 | NET_FS_FILE_MODE_OPEN, 1380 | NET_FS_FILE_ACCESS_RD); 1381 | break; 1382 | 1383 | 1384 | case TFTPc_FILE_OPEN_WR: 1385 | pfile = NetFS_FileOpen(p_filename, 1386 | NET_FS_FILE_MODE_CREATE, 1387 | NET_FS_FILE_ACCESS_WR); 1388 | break; 1389 | 1390 | 1391 | default: 1392 | break; 1393 | } 1394 | 1395 | return (pfile); 1396 | } 1397 | 1398 | 1399 | /* 1400 | ********************************************************************************************************* 1401 | * TFTPc_DataWr() 1402 | * 1403 | * Description : Write data to the file system. 1404 | * 1405 | * Argument(s) : p_err Pointer to variable that will receive the return error code from this function : 1406 | * 1407 | * TFTPc_ERR_NONE No error. 1408 | * TFTPc_ERR_FILE_WR Error writing to file. 1409 | * 1410 | * Return(s) : Number of octets written to file. 1411 | * 1412 | * Caller(s) : TFTPc_StateDataGet(). 1413 | * 1414 | * Note(s) : none. 1415 | ********************************************************************************************************* 1416 | */ 1417 | 1418 | static CPU_INT16U TFTPc_DataWr (TFTPc_ERR *p_err) 1419 | { 1420 | CPU_SIZE_T rx_data_len; 1421 | CPU_SIZE_T wr_data_len; 1422 | 1423 | 1424 | rx_data_len = TFTPc_RxPktLen - TFTP_PKT_SIZE_OPCODE - TFTP_PKT_SIZE_BLK_NBR; 1425 | wr_data_len = 0; 1426 | 1427 | if (rx_data_len > 0) { 1428 | (void)NetFS_FileWr((void *) TFTPc_FileHandle, 1429 | (void *)&TFTPc_RxPktBuf[TFTP_PKT_OFFSET_DATA], 1430 | (CPU_SIZE_T ) rx_data_len, 1431 | (CPU_SIZE_T *)&wr_data_len); 1432 | } 1433 | 1434 | if (wr_data_len != rx_data_len) { 1435 | *p_err = TFTPc_ERR_FILE_WR; 1436 | } else { 1437 | *p_err = TFTPc_ERR_NONE; 1438 | } 1439 | 1440 | return ((CPU_INT16U)wr_data_len); 1441 | } 1442 | 1443 | 1444 | /* 1445 | ********************************************************************************************************* 1446 | * TFTPc_DataRd() 1447 | * 1448 | * Description : Read data from the file system. 1449 | * 1450 | * Argument(s) : p_err Pointer to variable that will receive the return error code from this function : 1451 | * 1452 | * TFTPc_ERR_NONE No error. 1453 | * TFTPc_ERR_FILE_RD Error reading file. 1454 | * 1455 | * Return(s) : Number of octets read from file. 1456 | * 1457 | * Caller(s) : TFTPc_StateDataPut(). 1458 | * 1459 | * Note(s) : none. 1460 | ********************************************************************************************************* 1461 | */ 1462 | 1463 | static CPU_INT16U TFTPc_DataRd (TFTPc_ERR *p_err) 1464 | { 1465 | CPU_SIZE_T rd_data_len; 1466 | CPU_BOOLEAN err; 1467 | 1468 | 1469 | *p_err = TFTPc_ERR_NONE; 1470 | /* Rd data from file. */ 1471 | err = NetFS_FileRd((void *) TFTPc_FileHandle, 1472 | (void *)&TFTPc_TxPktBuf[TFTP_PKT_OFFSET_DATA], 1473 | (CPU_SIZE_T ) TFTPc_DATA_BLOCK_SIZE, 1474 | (CPU_SIZE_T *)&rd_data_len); 1475 | 1476 | if (rd_data_len == 0) { /* If NO data rd ... */ 1477 | if (err == DEF_FAIL) { /* ... and err occurred (NOT EOF), ... */ 1478 | *p_err = TFTPc_ERR_FILE_RD; /* ... rtn err. */ 1479 | } 1480 | } 1481 | 1482 | return ((CPU_INT16U)rd_data_len); 1483 | } 1484 | 1485 | 1486 | /* 1487 | ********************************************************************************************************* 1488 | * TFTPc_RxPkt() 1489 | * 1490 | * Description : Receive TFTP packet. 1491 | * 1492 | * Argument(s) : sock_id Socket descriptor/handle identifier of socket to receive data. 1493 | * 1494 | * p_pkt Pointer to packet to receive. 1495 | * 1496 | * pkt_len Length of packet to receive (in octets). 1497 | * 1498 | * p_err Pointer to variable that will receive the return error code from this function : 1499 | * 1500 | * TFTPc_ERR_NONE Packet successfully transmitted. 1501 | * TFTPc_ERR_RX_TIMEOUT Receive timeout. 1502 | * TFTPc_ERR_RX Error receiving packet. 1503 | * 1504 | * Return(s) : Number of positive data octets received, if NO errors. 1505 | * 1506 | * NET_SOCK_BSD_RTN_CODE_CONN_CLOSED, if socket connection closed. 1507 | * 1508 | * NET_SOCK_BSD_ERR_RX, otherwise. 1509 | * 1510 | * Caller(s) : TFTPc_Processing(). 1511 | * 1512 | * Note(s) : (1) #### When returning from the receive function, we should make sure the received 1513 | * packet comes from the host we are connected to. 1514 | * 1515 | * (2) #### Transitory errors (NET_ERR_RX) should probably trigger another attempt to 1516 | * transmit the packet, instead of returning an error right away. 1517 | ********************************************************************************************************* 1518 | */ 1519 | 1520 | static NET_SOCK_RTN_CODE TFTPc_RxPkt (NET_SOCK_ID sock_id, 1521 | void *p_pkt, 1522 | CPU_INT16U pkt_len, 1523 | TFTPc_ERR *p_err) 1524 | { 1525 | #ifdef NET_IPv4_MODULE_EN 1526 | NET_SOCK_ADDR_IPv4 *p_addrv4; 1527 | NET_SOCK_ADDR_IPv4 *p_serverv4; 1528 | #endif 1529 | #ifdef NET_IPv6_MODULE_EN 1530 | NET_SOCK_ADDR_IPv6 *p_addrv6; 1531 | NET_SOCK_ADDR_IPv6 *p_serverv6; 1532 | #endif 1533 | NET_SOCK_RTN_CODE rtn_code; 1534 | NET_SOCK_ADDR server_sock_addr_ip; 1535 | NET_SOCK_ADDR_LEN server_sock_addr_ip_len; 1536 | NET_ERR err; 1537 | 1538 | /* --------------- RX PKT THROUGH SOCK ---------------- */ 1539 | /* See Note #1. */ 1540 | server_sock_addr_ip_len = sizeof(server_sock_addr_ip); 1541 | rtn_code = NetSock_RxDataFrom((NET_SOCK_ID ) sock_id, 1542 | (void *) p_pkt, 1543 | (CPU_INT16U ) pkt_len, 1544 | (CPU_INT16S ) NET_SOCK_FLAG_NONE, 1545 | (NET_SOCK_ADDR *)&server_sock_addr_ip, 1546 | (NET_SOCK_ADDR_LEN *)&server_sock_addr_ip_len, 1547 | (void *) 0, 1548 | (CPU_INT08U ) 0, 1549 | (CPU_INT08U *) 0, 1550 | (NET_ERR *)&err); 1551 | switch (err) { 1552 | case NET_SOCK_ERR_NONE: 1553 | *p_err = TFTPc_ERR_NONE; 1554 | if (TFTPc_TID_Set != DEF_YES) { /* If terminal ID NOT set, ... */ 1555 | /* ... change server port to last rx'd one. */ 1556 | switch(TFTPc_SockAddr.AddrFamily) { 1557 | #ifdef NET_IPv4_MODULE_EN 1558 | case NET_SOCK_ADDR_FAMILY_IP_V4: 1559 | p_addrv4 = (NET_SOCK_ADDR_IPv4 *)&TFTPc_SockAddr; 1560 | p_serverv4 = (NET_SOCK_ADDR_IPv4 *)&server_sock_addr_ip; 1561 | p_addrv4->Port = p_serverv4->Port; 1562 | break; 1563 | #endif 1564 | #ifdef NET_IPv6_MODULE_EN 1565 | case NET_SOCK_ADDR_FAMILY_IP_V6: 1566 | p_addrv6 = (NET_SOCK_ADDR_IPv6 *)&TFTPc_SockAddr; 1567 | p_serverv6 = (NET_SOCK_ADDR_IPv6 *)&server_sock_addr_ip; 1568 | p_addrv6->Port = p_serverv6->Port; 1569 | break; 1570 | #endif 1571 | 1572 | default: 1573 | return (TFTPc_ERR_INVALID_PROTO_FAMILY); 1574 | } 1575 | 1576 | TFTPc_TID_Set = DEF_YES; 1577 | } 1578 | break; 1579 | 1580 | case NET_SOCK_ERR_RX_Q_EMPTY: 1581 | *p_err = TFTPc_ERR_RX_TIMEOUT; 1582 | break; 1583 | 1584 | case NET_ERR_RX: /* See Note #2. */ 1585 | default: 1586 | *p_err = TFTPc_ERR_RX; 1587 | break; 1588 | } 1589 | 1590 | return (rtn_code); 1591 | } 1592 | 1593 | 1594 | /* 1595 | ********************************************************************************************************* 1596 | * TFTPc_TxReq() 1597 | * 1598 | * Description : Transmit TFTP request packet. 1599 | * 1600 | * Argument(s) : req_opcode Opcode for this request : 1601 | * 1602 | * TFTP_OPCODE_RRQ Read request. 1603 | * TFTP_OPCODE_WRQ Write request. 1604 | * 1605 | * p_filename Name of the file to transfer. 1606 | * 1607 | * mode TFTP mode : 1608 | * 1609 | * TFTPc_MODE_NETASCII ASCII mode. 1610 | * TFTPc_MODE_OCTET Binary mode. 1611 | * 1612 | * p_err Pointer to variable that will receive the return error code from this function : 1613 | * 1614 | * TFTPc_ERR_NONE Request packet successfully transmitted. 1615 | * TFTPc_ERR_NULL_PTR Argument 'filename' passed a NULL pointer. 1616 | * TFTPc_ERR_INVALID_OPCODE Argument 'req_opcode' passed an invalid opcode. 1617 | * TFTPc_ERR_INVALID_MODE Argument 'mode' passed an invalid mode. 1618 | * 1619 | * --------- RETURNED BY TFTPc_TxPkt() : ---------- 1620 | * TFTPc_ERR_TX Error transmitting packet. 1621 | * 1622 | * Return(s) : none. 1623 | * 1624 | * Caller(s) : TFTPc_Get(), 1625 | * TFTPc_Put(). 1626 | * 1627 | * Note(s) : (1) RFC #1350, section 1 'Purpose' states that "the mail mode is obsolete and should not 1628 | * be implemented or used". 1629 | ********************************************************************************************************* 1630 | */ 1631 | 1632 | static void TFTPc_TxReq (CPU_INT16U req_opcode, 1633 | CPU_CHAR *p_filename, 1634 | TFTPc_MODE mode, 1635 | TFTPc_ERR *p_err) 1636 | { 1637 | CPU_CHAR *pmode_str; 1638 | CPU_INT16U filename_len; 1639 | CPU_INT16U mode_len; 1640 | CPU_INT16U wr_pkt_ix; 1641 | NET_SOCK_ADDR_LEN sock_addr_size; 1642 | 1643 | 1644 | /* ------------------ VALIDATE ARGS ------------------- */ 1645 | if (p_filename == (CPU_CHAR *)0) { /* If filename is NULL, ... */ 1646 | *p_err = TFTPc_ERR_NULL_PTR; /* ... rtn err. */ 1647 | return; 1648 | } 1649 | 1650 | if ((req_opcode != TFTP_OPCODE_RRQ) && /* If opcode not RRQ or WRQ, ... */ 1651 | (req_opcode != TFTP_OPCODE_WRQ)) { 1652 | *p_err = TFTPc_ERR_INVALID_OPCODE; /* ... rtn err. */ 1653 | return; 1654 | } 1655 | 1656 | switch (mode) { 1657 | case TFTPc_MODE_NETASCII: 1658 | pmode_str = TFTP_MODE_NETASCII_STR; 1659 | break; 1660 | 1661 | 1662 | case TFTPc_MODE_OCTET: 1663 | pmode_str = TFTP_MODE_BINARY_STR; 1664 | break; 1665 | 1666 | 1667 | case TFTPc_MODE_MAIL: /* See Note #1. */ 1668 | default: 1669 | *p_err = TFTPc_ERR_INVALID_MODE; 1670 | return; 1671 | } 1672 | 1673 | 1674 | 1675 | /* -------------------- CREATE PKT -------------------- */ 1676 | /* Wr opcode. */ 1677 | NET_UTIL_VAL_SET_NET_16(&TFTPc_TxPktBuf[TFTP_PKT_OFFSET_OPCODE], 1678 | req_opcode); 1679 | 1680 | /* Copy filename. */ 1681 | Str_Copy((CPU_CHAR *)&TFTPc_TxPktBuf[TFTP_PKT_OFFSET_FILENAME], 1682 | (CPU_CHAR *) p_filename); 1683 | 1684 | /* Wr mode. */ 1685 | filename_len = Str_Len(p_filename); 1686 | wr_pkt_ix = TFTP_PKT_OFFSET_FILENAME + 1687 | filename_len + 1688 | TFTP_PKT_SIZE_NULL; 1689 | 1690 | Str_Copy((CPU_CHAR *)&TFTPc_TxPktBuf[wr_pkt_ix], 1691 | (CPU_CHAR *) pmode_str); 1692 | 1693 | mode_len = Str_Len(pmode_str); 1694 | 1695 | /* Get total pkt size. */ 1696 | TFTPc_TxPktLen = wr_pkt_ix + 1697 | mode_len + 1698 | TFTP_PKT_SIZE_NULL; 1699 | 1700 | 1701 | /* --------------------- TX PKT ---------------------- */ 1702 | sock_addr_size = sizeof(NET_SOCK_ADDR); 1703 | (void)TFTPc_TxPkt((NET_SOCK_ID ) TFTPc_SockID, 1704 | (void *)&TFTPc_TxPktBuf[0], 1705 | (CPU_INT16U ) TFTPc_TxPktLen, 1706 | (NET_SOCK_ADDR *)&TFTPc_SockAddr, 1707 | (NET_SOCK_ADDR_LEN) sock_addr_size, 1708 | (TFTPc_ERR *) p_err); 1709 | } 1710 | 1711 | 1712 | /* 1713 | ********************************************************************************************************* 1714 | * TFTPc_TxData() 1715 | * 1716 | * Description : Transmit TFTP data packet. 1717 | * 1718 | * Argument(s) : blk_nbr Block number for data packet. 1719 | * 1720 | * data_len Length of data portion of packet (in octets). 1721 | * 1722 | * p_err Pointer to variable that will receive the return error code from this function : 1723 | * 1724 | * TFTPc_ERR_NONE Data packet successfully transmitted. 1725 | * 1726 | * ------------- RETURNED BY TFTPc_TxPkt() : -------------- 1727 | * TFTPc_ERR_TX Error transmitting packet. 1728 | * 1729 | * Return(s) : none. 1730 | * 1731 | * Caller(s) : TFTPc_DataRd(). 1732 | * 1733 | * Note(s) : none. 1734 | ********************************************************************************************************* 1735 | */ 1736 | 1737 | static void TFTPc_TxData (TFTPc_BLK_NBR blk_nbr, 1738 | CPU_INT16U data_len, 1739 | TFTPc_ERR *p_err) 1740 | { 1741 | NET_SOCK_ADDR_LEN sock_addr_size; 1742 | 1743 | 1744 | /* -------------------- CREATE PKT -------------------- */ 1745 | /* Wr opcode. */ 1746 | NET_UTIL_VAL_SET_NET_16(&TFTPc_TxPktBuf[TFTP_PKT_OFFSET_OPCODE], 1747 | TFTP_OPCODE_DATA); 1748 | 1749 | /* Wr blk nbr. */ 1750 | NET_UTIL_VAL_SET_NET_16(&TFTPc_TxPktBuf[TFTP_PKT_OFFSET_BLK_NBR], 1751 | blk_nbr); 1752 | 1753 | /* Get total pkt size. */ 1754 | TFTPc_TxPktLen = TFTP_PKT_SIZE_OPCODE + 1755 | TFTP_PKT_SIZE_BLK_NBR + 1756 | data_len; 1757 | 1758 | /* --------------------- TX PKT ---------------------- */ 1759 | sock_addr_size = sizeof(NET_SOCK_ADDR); 1760 | (void)TFTPc_TxPkt((NET_SOCK_ID ) TFTPc_SockID, 1761 | (void *)&TFTPc_TxPktBuf[0], 1762 | (CPU_INT16U ) TFTPc_TxPktLen, 1763 | (NET_SOCK_ADDR *)&TFTPc_SockAddr, 1764 | (NET_SOCK_ADDR_LEN) sock_addr_size, 1765 | (TFTPc_ERR *) p_err); 1766 | } 1767 | 1768 | 1769 | /* 1770 | ********************************************************************************************************* 1771 | * TFTPc_TxAck() 1772 | * 1773 | * Description : Transmit TFTP acknowledge packet. 1774 | * 1775 | * Argument(s) : blk_nbr Block number to acknowledge. 1776 | * 1777 | * p_err Pointer to variable that will receive the return error code from this function : 1778 | * 1779 | * TFTPc_ERR_NONE Acknowledge packet successfully transmitted. 1780 | * 1781 | * ------------- RETURNED BY TFTPc_TxPkt() : -------------- 1782 | * TFTPc_ERR_TX Error transmitting packet. 1783 | * 1784 | * Return(s) : none. 1785 | * 1786 | * Caller(s) : TFTPc_DataWr(). 1787 | * 1788 | * Note(s) : none. 1789 | ********************************************************************************************************* 1790 | */ 1791 | 1792 | static void TFTPc_TxAck (TFTPc_BLK_NBR blk_nbr, 1793 | TFTPc_ERR *p_err) 1794 | { 1795 | NET_SOCK_ADDR_LEN sock_addr_size; 1796 | 1797 | 1798 | NET_UTIL_VAL_SET_NET_16(&TFTPc_TxPktBuf[TFTP_PKT_OFFSET_OPCODE], 1799 | TFTP_OPCODE_ACK); 1800 | 1801 | NET_UTIL_VAL_SET_NET_16(&TFTPc_TxPktBuf[TFTP_PKT_OFFSET_BLK_NBR], 1802 | blk_nbr); 1803 | 1804 | TFTPc_TxPktLen = TFTP_PKT_SIZE_OPCODE + TFTP_PKT_SIZE_BLK_NBR; 1805 | 1806 | 1807 | /* --------------------- TX PKT ---------------------- */ 1808 | sock_addr_size = sizeof(NET_SOCK_ADDR); 1809 | (void)TFTPc_TxPkt((NET_SOCK_ID ) TFTPc_SockID, 1810 | (void *)&TFTPc_TxPktBuf[0], 1811 | (CPU_INT16U ) TFTPc_TxPktLen, 1812 | (NET_SOCK_ADDR *)&TFTPc_SockAddr, 1813 | (NET_SOCK_ADDR_LEN) sock_addr_size, 1814 | (TFTPc_ERR *) p_err); 1815 | } 1816 | 1817 | 1818 | /* 1819 | ********************************************************************************************************* 1820 | * TFTPc_TxErr() 1821 | * 1822 | * Description : Transmit TFTP error packet. 1823 | * 1824 | * Argument(s) : err_code Code indicating the nature of the error. 1825 | * 1826 | * p_err_msg String associated with error (terminated by NULL character). 1827 | * 1828 | * p_err Pointer to variable that will receive the return error code from this function : 1829 | * 1830 | * TFTPc_ERR_NONE Error packet successfully transmitted. 1831 | * 1832 | * ------------- RETURNED BY TFTPc_TxPkt() : -------------- 1833 | * TFTPc_ERR_TX Error transmitting packet. 1834 | * 1835 | * Return(s) : none. 1836 | * 1837 | * Caller(s) : TFTPc_StateDataGet(), 1838 | * TFTPc_StateDataPut(). 1839 | * 1840 | * Note(s) : none. 1841 | ********************************************************************************************************* 1842 | */ 1843 | 1844 | static void TFTPc_TxErr (CPU_INT16U err_code, 1845 | CPU_CHAR *p_err_msg, 1846 | TFTPc_ERR *p_err) 1847 | { 1848 | CPU_INT16U err_msg_len; 1849 | NET_SOCK_ADDR_LEN sock_addr_size; 1850 | 1851 | 1852 | NET_UTIL_VAL_SET_NET_16(&TFTPc_TxPktBuf[TFTP_PKT_OFFSET_OPCODE], 1853 | TFTP_OPCODE_ERR); 1854 | 1855 | NET_UTIL_VAL_SET_NET_16(&TFTPc_TxPktBuf[TFTP_PKT_OFFSET_ERR_CODE], 1856 | err_code); 1857 | 1858 | /* Copy err msg into tx pkt. */ 1859 | if (p_err_msg != (CPU_CHAR *)0) { 1860 | Str_Copy((CPU_CHAR *)&TFTPc_TxPktBuf[TFTP_PKT_OFFSET_ERR_MSG], 1861 | (CPU_CHAR *) p_err_msg); 1862 | 1863 | err_msg_len = Str_Len(p_err_msg); 1864 | 1865 | } else { 1866 | TFTPc_TxPktBuf[TFTP_PKT_OFFSET_ERR_MSG] = (CPU_CHAR)0; 1867 | err_msg_len = 0; 1868 | } 1869 | 1870 | TFTPc_TxPktLen = TFTP_PKT_SIZE_OPCODE + 1871 | TFTP_PKT_SIZE_ERR_CODE + 1872 | err_msg_len + 1873 | TFTP_PKT_SIZE_NULL; 1874 | 1875 | /* --------------------- TX PKT ---------------------- */ 1876 | sock_addr_size = sizeof(NET_SOCK_ADDR); 1877 | (void)TFTPc_TxPkt((NET_SOCK_ID ) TFTPc_SockID, 1878 | (void *)&TFTPc_TxPktBuf[0], 1879 | (CPU_INT16U ) TFTPc_TxPktLen, 1880 | (NET_SOCK_ADDR *)&TFTPc_SockAddr, 1881 | (NET_SOCK_ADDR_LEN) sock_addr_size, 1882 | (TFTPc_ERR *) p_err); 1883 | } 1884 | 1885 | 1886 | /* 1887 | ********************************************************************************************************* 1888 | * TFTPc_TxPkt() 1889 | * 1890 | * Description : Transmit TFTP packet. 1891 | * 1892 | * Argument(s) : sock_id Socket descriptor/handle identifier of socket to transmit data. 1893 | * 1894 | * p_pkt Pointer to packet to transmit. 1895 | * 1896 | * pkt_len Length of packet to transmit (in octets). 1897 | * 1898 | * p_addr_remote Pointer to destination address buffer. 1899 | * 1900 | * addr_len Length of destination address buffer (in octets). 1901 | * 1902 | * p_err Pointer to variable that will receive the return error code from this function : 1903 | * 1904 | * TFTPc_ERR_NONE Packet successfully transmitted. 1905 | * TFTPc_ERR_TX Error transmitting packet. 1906 | * 1907 | * Return(s) : Number of positive data octets transmitted, if NO error. 1908 | * 1909 | * NET_SOCK_BSD_RTN_CODE_CONN_CLOSED, if socket connection closed. 1910 | * 1911 | * NET_SOCK_BSD_ERR_TX, otherwise . 1912 | * 1913 | * Caller(s) : TFTPc_TxReq(), 1914 | * TFTPc_TxData(), 1915 | * TFTPc_TxAck(), 1916 | * TFTPc_TxErr(). 1917 | * 1918 | * Note(s) : (1) #### Transitory errors (NET_ERR_TX) should probably trigger another attempt to 1919 | * transmit the packet, instead of returning an error right away. 1920 | ********************************************************************************************************* 1921 | */ 1922 | 1923 | static NET_SOCK_RTN_CODE TFTPc_TxPkt (NET_SOCK_ID sock_id, 1924 | void *p_pkt, 1925 | CPU_INT16U pkt_len, 1926 | NET_SOCK_ADDR *p_addr_remote, 1927 | NET_SOCK_ADDR_LEN addr_len, 1928 | TFTPc_ERR *p_err) 1929 | { 1930 | NET_SOCK_RTN_CODE rtn_code; 1931 | NET_ERR err; 1932 | 1933 | /* --------------- TX PKT THROUGH SOCK ---------------- */ 1934 | rtn_code = NetSock_TxDataTo((NET_SOCK_ID ) sock_id, 1935 | (void *) p_pkt, 1936 | (CPU_INT16U ) pkt_len, 1937 | (CPU_INT16S ) NET_SOCK_FLAG_NONE, 1938 | (NET_SOCK_ADDR *) p_addr_remote, 1939 | (NET_SOCK_ADDR_LEN) addr_len, 1940 | (NET_ERR *)&err); 1941 | switch (err) { 1942 | case NET_SOCK_ERR_NONE: 1943 | *p_err = TFTPc_ERR_NONE; 1944 | break; 1945 | 1946 | case NET_ERR_TX: /* See Note #1. */ 1947 | default: 1948 | *p_err = TFTPc_ERR_TX; 1949 | break; 1950 | } 1951 | 1952 | return (rtn_code); 1953 | } 1954 | 1955 | 1956 | /* 1957 | ********************************************************************************************************* 1958 | * TFTPc_Terminate() 1959 | * 1960 | * Description : (1) Terminate the current file transfer process. 1961 | * 1962 | * (a) Set TFTP client state to 'COMPLETE' 1963 | * (b) Close opened file. 1964 | * 1965 | * 1966 | * Argument(s) : none. 1967 | * 1968 | * Return(s) : none. 1969 | * 1970 | * Caller(s) : TFTPc_Get(), 1971 | * TFTPc_Put(), 1972 | * TFTPc_Processing(). 1973 | * 1974 | * Note(s) : none. 1975 | ********************************************************************************************************* 1976 | */ 1977 | 1978 | static void TFTPc_Terminate (void) 1979 | { 1980 | NET_ERR err; 1981 | 1982 | 1983 | if (TFTPc_SockID != NET_SOCK_ID_NONE) { /* Close sock. */ 1984 | NetSock_Close(TFTPc_SockID, &err); 1985 | TFTPc_SockID = NET_SOCK_ID_NONE; 1986 | } 1987 | 1988 | if (TFTPc_FileHandle != (void *)0) { /* Close file. */ 1989 | NetFS_FileClose(TFTPc_FileHandle); 1990 | TFTPc_FileHandle = (void *)0; 1991 | } 1992 | } 1993 | -------------------------------------------------------------------------------- /Source/tftp-c.h: -------------------------------------------------------------------------------- 1 | /* 2 | ********************************************************************************************************* 3 | * uC/TFTPc 4 | * Trivial File Transfer Protocol (client) 5 | * 6 | * Copyright 2004-2020 Silicon Laboratories Inc. www.silabs.com 7 | * 8 | * SPDX-License-Identifier: APACHE-2.0 9 | * 10 | * This software is subject to an open source license and is distributed by 11 | * Silicon Laboratories Inc. pursuant to the terms of the Apache License, 12 | * Version 2.0 available at www.apache.org/licenses/LICENSE-2.0. 13 | * 14 | ********************************************************************************************************* 15 | */ 16 | 17 | /* 18 | ********************************************************************************************************* 19 | * 20 | * TFTP CLIENT 21 | * 22 | * Filename : tftp-c.h 23 | * Version : V2.01.00 24 | ********************************************************************************************************* 25 | * Note(s) : (1) Assumes the following versions (or more recent) of software modules are included in 26 | * the project build : 27 | * 28 | * (a) uC/CPU V1.29.02 29 | * (b) uC/LIB V1.38.00 30 | * (c) uC/Common V1.00.00 31 | * (d) uC/TCP-IP V3.02.00 32 | * 33 | * 34 | * (2) For additional details on the features available with uC/TFTPc, the API, the 35 | * installation, etc. Please refer to the uC/TFTPc documentation available at 36 | * https://doc.micrium.com. 37 | ********************************************************************************************************* 38 | */ 39 | 40 | /* 41 | ********************************************************************************************************* 42 | ********************************************************************************************************* 43 | * MODULE 44 | * 45 | * Note(s) : (1) This header file is protected from multiple pre-processor inclusion through use of the 46 | * TFTPc present pre-processor macro definition. 47 | ********************************************************************************************************* 48 | ********************************************************************************************************* 49 | */ 50 | 51 | #ifndef TFTPc_MODULE_PRESENT /* See Note #1. */ 52 | #define TFTPc_MODULE_PRESENT 53 | 54 | 55 | /* 56 | ********************************************************************************************************* 57 | ********************************************************************************************************* 58 | * TFTPs VERSION NUMBER 59 | * 60 | * Note(s) : (1) (a) The TFTPs module software version is denoted as follows : 61 | * 62 | * Vx.yy.zz 63 | * 64 | * where 65 | * V denotes 'Version' label 66 | * x denotes major software version revision number 67 | * yy denotes minor software version revision number 68 | * zz denotes sub-minor software version revision number 69 | * 70 | * (b) The TFTPs software version label #define is formatted as follows : 71 | * 72 | * ver = x.yyzz * 100 * 100 73 | * 74 | * where 75 | * ver denotes software version number scaled as an integer value 76 | * x.yyzz denotes software version number, where the unscaled integer 77 | * portion denotes the major version number & the unscaled 78 | * fractional portion denotes the (concatenated) minor 79 | * version numbers 80 | ********************************************************************************************************* 81 | ********************************************************************************************************* 82 | */ 83 | 84 | #define TFTPc_VERSION 20100u /* See Note #1. */ 85 | 86 | 87 | /* 88 | ********************************************************************************************************* 89 | ********************************************************************************************************* 90 | * EXTERNS 91 | ********************************************************************************************************* 92 | ********************************************************************************************************* 93 | */ 94 | 95 | #ifdef TFTPc_MODULE 96 | #define TFTPc_EXT 97 | #else 98 | #define TFTPc_EXT extern 99 | #endif 100 | 101 | 102 | /* 103 | ********************************************************************************************************* 104 | ********************************************************************************************************* 105 | * INCLUDE FILES 106 | * 107 | * Note(s) : (1) The TFTPc module files are located in the following directories : 108 | * 109 | * (a) \\tftp-c_cfg.h 110 | * 111 | * (b) \\Source\net_*.* 112 | * \FS\net_fs.h 113 | * 114 | * (c) (1) \\Source\tftp-c.h 115 | * \tftp-c.c 116 | * 117 | * (2) CPU-configuration software files are located in the following directories : 118 | * 119 | * (a) \\cpu_*.* 120 | * (b) \\\\cpu*.* 121 | * 122 | * where 123 | * directory path for common CPU-compiler software 124 | * directory name for specific processor (CPU) 125 | * directory name for specific compiler 126 | * 127 | * (3) NO compiler-supplied standard library functions SHOULD be used. 128 | * 129 | * (a) Standard library functions are implemented in the custom library module(s) : 130 | * 131 | * \\lib_*.* 132 | * 133 | * where 134 | * directory path for custom library software 135 | * 136 | * (4) Compiler MUST be configured to include as additional include path directories : 137 | * 138 | * (a) '\\' directory See Note #1a 139 | * 140 | * (b) '\\' directory See Note #1b 141 | * 142 | * (c) '\\' directories See Note #1c 143 | * 144 | * (d) (1) '\\' directory See Note #2a 145 | * (2) '\\\\' directory See Note #2b 146 | * 147 | * (e) '\\' directory See Note #3a 148 | ********************************************************************************************************* 149 | ********************************************************************************************************* 150 | */ 151 | 152 | #include /* CPU Configuration (see Note #2b) */ 153 | #include /* CPU Core Library (see Note #2a) */ 154 | 155 | #include /* Standard Defines (see Note #3a) */ 156 | #include /* Standard String Library (see Note #3a) */ 157 | 158 | #include /* TFTP Client Configuration File (see Note #1a) */ 159 | 160 | #include /* File System Interface (see Note #1b) */ 161 | 162 | #include /* Network Protocol Suite (see Note #1b) */ 163 | #include 164 | 165 | #if 1 166 | #include 167 | #endif 168 | 169 | /* 170 | ********************************************************************************************************* 171 | ********************************************************************************************************* 172 | * DEFINES 173 | ********************************************************************************************************* 174 | ********************************************************************************************************* 175 | */ 176 | 177 | /* 178 | ********************************************************************************************************* 179 | * TFTPc TRANSFER MODE DEFINES 180 | ********************************************************************************************************* 181 | */ 182 | 183 | #define TFTPc_MODE_NETASCII 1 184 | #define TFTPc_MODE_OCTET 2 185 | #define TFTPc_MODE_MAIL 3 186 | 187 | 188 | /* 189 | ********************************************************************************************************* 190 | ********************************************************************************************************* 191 | * DATA TYPES 192 | ********************************************************************************************************* 193 | ********************************************************************************************************* 194 | */ 195 | 196 | /* 197 | ********************************************************************************************************* 198 | * TFTPc ERROR CODES DATA TYPE 199 | ********************************************************************************************************* 200 | */ 201 | 202 | typedef enum tftpc_err { 203 | TFTPc_ERR_NONE, 204 | 205 | TFTPc_ERR_LOCK, /* TFTPc Lock error. */ 206 | TFTPc_ERR_FAULT_INIT, /* Initialization faulted. */ 207 | TFTPc_ERR_MEM_ALLOC, /* Memory allocation error. */ 208 | TFTPc_ERR_CFG_INVALID, /* Invalid Configuration. */ 209 | 210 | TFTPc_ERR_NO_SOCK, /* No socket available. */ 211 | TFTPc_ERR_RX, /* Rx err. */ 212 | TFTPc_ERR_RX_TIMEOUT, /* Rx timeout. */ 213 | TFTPc_ERR_ERR_PKT_RX, /* Err pkt rx'd. */ 214 | TFTPc_ERR_TX, /* Tx err. */ 215 | TFTPc_ERR_NULL_PTR, /* Ptr arg(s) passed NULL ptr(s). */ 216 | TFTPc_ERR_INVALID_MODE, /* Invalid TFTP transfer mode. */ 217 | TFTPc_ERR_INVALID_OPCODE, /* Invalid opcode for req. */ 218 | TFTPc_ERR_INVALID_OPCODE_RX, /* Invalid opcode rx'd. */ 219 | TFTPC_ERR_FILE_OPEN, /* Could not open the specified file. */ 220 | TFTPc_ERR_FILE_RD, /* Err rd'ing from file. */ 221 | TFTPc_ERR_FILE_WR, /* Err wr'ing to file. */ 222 | TFTPc_ERR_INVALID_STATE, /* Invalid state for TFTP client state machine. */ 223 | TFTPc_ERR_INVALID_PROTO_FAMILY /* Invalid or unsupported protocol family. */ 224 | } TFTPc_ERR; 225 | 226 | 227 | /* 228 | ********************************************************************************************************* 229 | * TFTPc MODE DATA TYPE 230 | ********************************************************************************************************* 231 | */ 232 | 233 | typedef CPU_INT08U TFTPc_MODE; 234 | 235 | 236 | /* 237 | ********************************************************************************************************* 238 | ********************************************************************************************************* 239 | * FUNCTION PROTOTYPES 240 | ********************************************************************************************************* 241 | ********************************************************************************************************* 242 | */ 243 | 244 | CPU_BOOLEAN TFTPc_Init (const TFTPc_CFG *p_cfg, 245 | TFTPc_ERR *p_err); 246 | 247 | CPU_BOOLEAN TFTPc_SetDfltCfg (const TFTPc_CFG *p_cfg, 248 | TFTPc_ERR *p_err); 249 | 250 | CPU_BOOLEAN TFTPc_Get (const TFTPc_CFG *p_cfg, 251 | CPU_CHAR *p_filename_local, 252 | CPU_CHAR *p_filename_remote, 253 | TFTPc_MODE mode, 254 | TFTPc_ERR *p_err); 255 | 256 | CPU_BOOLEAN TFTPc_Put (const TFTPc_CFG *p_cfg, 257 | CPU_CHAR *p_filename_local, 258 | CPU_CHAR *p_filename_remote, 259 | TFTPc_MODE mode, 260 | TFTPc_ERR *p_err); 261 | 262 | 263 | /* 264 | ********************************************************************************************************* 265 | ********************************************************************************************************* 266 | * TRACING 267 | ********************************************************************************************************* 268 | ********************************************************************************************************* 269 | */ 270 | 271 | /* Trace level, default to TRACE_LEVEL_OFF */ 272 | #ifndef TRACE_LEVEL_OFF 273 | #define TRACE_LEVEL_OFF 0 274 | #endif 275 | 276 | #ifndef TRACE_LEVEL_INFO 277 | #define TRACE_LEVEL_INFO 1 278 | #endif 279 | 280 | #ifndef TRACE_LEVEL_DBG 281 | #define TRACE_LEVEL_DBG 2 282 | #endif 283 | 284 | #ifndef TFTPc_TRACE_LEVEL 285 | #define TFTPc_TRACE_LEVEL TRACE_LEVEL_OFF 286 | #endif 287 | 288 | #ifndef TFTPc_TRACE 289 | #define TFTPc_TRACE printf 290 | #endif 291 | 292 | #if ((defined(TFTPc_TRACE)) && \ 293 | (defined(TFTPc_TRACE_LEVEL)) && \ 294 | (TFTPc_TRACE_LEVEL >= TRACE_LEVEL_INFO) ) 295 | 296 | #if (TFTPc_TRACE_LEVEL >= TRACE_LEVEL_DBG) 297 | #define TFTPc_TRACE_DBG(msg) TFTPc_TRACE msg 298 | #else 299 | #define TFTPc_TRACE_DBG(msg) 300 | #endif 301 | 302 | #define TFTPc_TRACE_INFO(msg) TFTPc_TRACE msg 303 | 304 | #else 305 | #define TFTPc_TRACE_DBG(msg) 306 | #define TFTPc_TRACE_INFO(msg) 307 | #endif 308 | 309 | 310 | /* 311 | ********************************************************************************************************* 312 | ********************************************************************************************************* 313 | * MODULE END 314 | ********************************************************************************************************* 315 | ********************************************************************************************************* 316 | */ 317 | 318 | #endif /* TFTPc_MODULE_PRESENT */ 319 | 320 | -------------------------------------------------------------------------------- /Source/tftp-c_type.h: -------------------------------------------------------------------------------- 1 | /* 2 | ********************************************************************************************************* 3 | * uC/TFTPc 4 | * Trivial File Transfer Protocol (client) 5 | * 6 | * Copyright 2004-2020 Silicon Laboratories Inc. www.silabs.com 7 | * 8 | * SPDX-License-Identifier: APACHE-2.0 9 | * 10 | * This software is subject to an open source license and is distributed by 11 | * Silicon Laboratories Inc. pursuant to the terms of the Apache License, 12 | * Version 2.0 available at www.apache.org/licenses/LICENSE-2.0. 13 | * 14 | ********************************************************************************************************* 15 | */ 16 | 17 | /* 18 | ********************************************************************************************************* 19 | * 20 | * TFTP CLIENT TYPE MODULE 21 | * 22 | * Filename : tftp-c_type.h 23 | * Version : V2.01.00 24 | ********************************************************************************************************* 25 | * Note(s) : (1) Assumes the following versions (or more recent) of software modules are included in 26 | * the project build : 27 | * 28 | * (a) uC/CPU V1.29.02 29 | * (b) uC/LIB V1.38.00 30 | * (c) uC/Common V1.00.00 31 | * (d) uC/TCP-IP V3.02.00 32 | * 33 | * 34 | * (2) For additional details on the features available with uC/TFTPc, the API, the 35 | * installation, etc. Please refer to the uC/TFTPc documentation available at 36 | * https://doc.micrium.com. 37 | ********************************************************************************************************* 38 | */ 39 | 40 | /* 41 | ********************************************************************************************************* 42 | ********************************************************************************************************* 43 | * MODULE 44 | * 45 | * Note(s) : (1) This header file is protected from multiple pre-processor inclusion through use of the 46 | * TFTPc present pre-processor macro definition. 47 | ********************************************************************************************************* 48 | ********************************************************************************************************* 49 | */ 50 | 51 | #ifndef TFTPc_TYPE_MODULE_PRESENT /* See Note #1. */ 52 | #define TFTPc_TYPE_MODULE_PRESENT 53 | 54 | 55 | /* 56 | ********************************************************************************************************* 57 | ********************************************************************************************************* 58 | * INCLUDE FILES 59 | ********************************************************************************************************* 60 | ********************************************************************************************************* 61 | */ 62 | 63 | #include 64 | #include 65 | 66 | 67 | /* 68 | ********************************************************************************************************* 69 | ********************************************************************************************************* 70 | * DATA TYPES 71 | ********************************************************************************************************* 72 | ********************************************************************************************************* 73 | */ 74 | 75 | /* 76 | ********************************************************************************************************* 77 | * TFTPc CONFIGURATION DATA TYPE 78 | ********************************************************************************************************* 79 | */ 80 | 81 | typedef struct tftpc_cfg { 82 | CPU_CHAR *ServerHostnamePtr; 83 | NET_PORT_NBR ServerPortNbr; 84 | NET_IP_ADDR_FAMILY ServerAddrFamily; 85 | 86 | CPU_INT32U RxInactivityTimeout_ms; 87 | CPU_INT32U TxInactivityTimeout_ms; 88 | } TFTPc_CFG; 89 | 90 | 91 | /* 92 | ********************************************************************************************************* 93 | ********************************************************************************************************* 94 | * MODULE END 95 | ********************************************************************************************************* 96 | ********************************************************************************************************* 97 | */ 98 | 99 | #endif /* TFTPc_TYPE_MODULE_PRESENT */ 100 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # uC/TFTPc 2 | 3 | µC/TFTPc is the Micriµm Trivial File Transport Protocol client. It is part of the Micriµm TFTP products suite. 4 | 5 | ## For the complete documentation, visit https://doc.micrium.com/display/ucos/ --------------------------------------------------------------------------------