Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: ppCANOpen_Example DISCO-F746NG_rtos_test
source/ServiceProvider.cpp@3:12b3c25bdeba, 2016-01-04 (annotated)
- Committer:
- ptpaterson
- Date:
- Mon Jan 04 06:10:49 2016 +0000
- Revision:
- 3:12b3c25bdeba
- Parent:
- 2:c724ff3a4e4d
- Child:
- 4:2034b04c86d2
Echo communication verified
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| ptpaterson | 2:c724ff3a4e4d | 1 | /** | 
| ptpaterson | 2:c724ff3a4e4d | 2 | ****************************************************************************** | 
| ptpaterson | 2:c724ff3a4e4d | 3 | * @file | 
| ptpaterson | 2:c724ff3a4e4d | 4 | * @author Paul Paterson | 
| ptpaterson | 2:c724ff3a4e4d | 5 | * @version | 
| ptpaterson | 2:c724ff3a4e4d | 6 | * @date 2015-12-22 | 
| ptpaterson | 2:c724ff3a4e4d | 7 | * @brief CANOpen implementation library | 
| ptpaterson | 2:c724ff3a4e4d | 8 | ****************************************************************************** | 
| ptpaterson | 2:c724ff3a4e4d | 9 | * @attention | 
| ptpaterson | 2:c724ff3a4e4d | 10 | * | 
| ptpaterson | 2:c724ff3a4e4d | 11 | * <h2><center>© COPYRIGHT(c) 2015 Paul Paterson | 
| ptpaterson | 2:c724ff3a4e4d | 12 | * | 
| ptpaterson | 2:c724ff3a4e4d | 13 | * All rights reserved. | 
| ptpaterson | 2:c724ff3a4e4d | 14 | |
| ptpaterson | 2:c724ff3a4e4d | 15 | This program is free software: you can redistribute it and/or modify | 
| ptpaterson | 2:c724ff3a4e4d | 16 | it under the terms of the GNU General Public License as published by | 
| ptpaterson | 2:c724ff3a4e4d | 17 | the Free Software Foundation, either version 3 of the License, or | 
| ptpaterson | 2:c724ff3a4e4d | 18 | (at your option) any later version. | 
| ptpaterson | 2:c724ff3a4e4d | 19 | |
| ptpaterson | 2:c724ff3a4e4d | 20 | This program is distributed in the hope that it will be useful, | 
| ptpaterson | 2:c724ff3a4e4d | 21 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| ptpaterson | 2:c724ff3a4e4d | 22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
| ptpaterson | 2:c724ff3a4e4d | 23 | GNU General Public License for more details. | 
| ptpaterson | 2:c724ff3a4e4d | 24 | |
| ptpaterson | 2:c724ff3a4e4d | 25 | You should have received a copy of the GNU General Public License | 
| ptpaterson | 2:c724ff3a4e4d | 26 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 
| ptpaterson | 2:c724ff3a4e4d | 27 | */ | 
| ptpaterson | 2:c724ff3a4e4d | 28 | |
| ptpaterson | 2:c724ff3a4e4d | 29 | #include "ServiceProvider.h" | 
| ptpaterson | 2:c724ff3a4e4d | 30 | #include "Node.h" | 
| ptpaterson | 2:c724ff3a4e4d | 31 | |
| ptpaterson | 2:c724ff3a4e4d | 32 | #include "canopen_api.h" | 
| ptpaterson | 2:c724ff3a4e4d | 33 | |
| ptpaterson | 2:c724ff3a4e4d | 34 | #include <string.h> | 
| ptpaterson | 3:12b3c25bdeba | 35 | #include <stdio.h> | 
| ptpaterson | 2:c724ff3a4e4d | 36 | |
| ptpaterson | 2:c724ff3a4e4d | 37 | namespace ppCANOpen | 
| ptpaterson | 2:c724ff3a4e4d | 38 | { | 
| ptpaterson | 2:c724ff3a4e4d | 39 | |
| ptpaterson | 2:c724ff3a4e4d | 40 | ServiceProvider::ServiceProvider (void) : nodeCount(0) | 
| ptpaterson | 2:c724ff3a4e4d | 41 | { | 
| ptpaterson | 3:12b3c25bdeba | 42 | printf ("-------- SERVICE PROVIDER: CONSTRUCTOR\r\n"); | 
| ptpaterson | 3:12b3c25bdeba | 43 | |
| ptpaterson | 3:12b3c25bdeba | 44 | //memset (nodes, 0, sizeof(Node) * SERVICE_MAX_NODES ); // TODO: fix incorrect <-- | 
| ptpaterson | 3:12b3c25bdeba | 45 | for (int i = 0; i < SERVICE_MAX_NODES; i++) { | 
| ptpaterson | 3:12b3c25bdeba | 46 | nodes[i] = 0; | 
| ptpaterson | 3:12b3c25bdeba | 47 | } | 
| ptpaterson | 3:12b3c25bdeba | 48 | printf ("-------- SERVICE PROVIDER: MEMSET GOOD\r\n"); | 
| ptpaterson | 3:12b3c25bdeba | 49 | |
| ptpaterson | 3:12b3c25bdeba | 50 | hCanOpen = new CanOpenHandle; | 
| ptpaterson | 3:12b3c25bdeba | 51 | CanOpenApiInit(hCanOpen); | 
| ptpaterson | 3:12b3c25bdeba | 52 | } | 
| ptpaterson | 3:12b3c25bdeba | 53 | |
| ptpaterson | 3:12b3c25bdeba | 54 | ServiceProvider::~ServiceProvider (void) | 
| ptpaterson | 3:12b3c25bdeba | 55 | { | 
| ptpaterson | 3:12b3c25bdeba | 56 | delete hCanOpen; | 
| ptpaterson | 2:c724ff3a4e4d | 57 | } | 
| ptpaterson | 2:c724ff3a4e4d | 58 | |
| ptpaterson | 2:c724ff3a4e4d | 59 | /*============================================================================= | 
| ptpaterson | 2:c724ff3a4e4d | 60 | * main loop | 
| ptpaterson | 2:c724ff3a4e4d | 61 | *============================================================================= | 
| ptpaterson | 2:c724ff3a4e4d | 62 | */ | 
| ptpaterson | 2:c724ff3a4e4d | 63 | |
| ptpaterson | 2:c724ff3a4e4d | 64 | void ServiceProvider::Run (void) | 
| ptpaterson | 2:c724ff3a4e4d | 65 | { | 
| ptpaterson | 3:12b3c25bdeba | 66 | printf ("-------- SERVICE PROVIDER: RUN\r\n"); | 
| ptpaterson | 3:12b3c25bdeba | 67 | |
| ptpaterson | 2:c724ff3a4e4d | 68 | while (1) { | 
| ptpaterson | 3:12b3c25bdeba | 69 | |
| ptpaterson | 2:c724ff3a4e4d | 70 | /* Check for new messages */ | 
| ptpaterson | 2:c724ff3a4e4d | 71 | CanOpenMessage msg; | 
| ptpaterson | 3:12b3c25bdeba | 72 | if (hCanOpen->can) { | 
| ptpaterson | 3:12b3c25bdeba | 73 | if (CanOpenApiRead (hCanOpen, &msg)) { | 
| ptpaterson | 3:12b3c25bdeba | 74 | |
| ptpaterson | 3:12b3c25bdeba | 75 | /* if new message exists, give it to the nodes*/ | 
| ptpaterson | 3:12b3c25bdeba | 76 | /* update all of the nodes */ | 
| ptpaterson | 3:12b3c25bdeba | 77 | for (int i=0; i < nodeCount; i++) { | 
| ptpaterson | 3:12b3c25bdeba | 78 | nodes[i]->DispatchMessage(&msg); | 
| ptpaterson | 3:12b3c25bdeba | 79 | } | 
| ptpaterson | 3:12b3c25bdeba | 80 | |
| ptpaterson | 3:12b3c25bdeba | 81 | //Node::DispatchMessage(&msg); | 
| ptpaterson | 3:12b3c25bdeba | 82 | |
| ptpaterson | 3:12b3c25bdeba | 83 | //**ECHO** | 
| ptpaterson | 3:12b3c25bdeba | 84 | if (CanOpenApiWrite(hCanOpen, &msg)) { | 
| ptpaterson | 3:12b3c25bdeba | 85 | } | 
| ptpaterson | 3:12b3c25bdeba | 86 | //**END ECHO | 
| ptpaterson | 3:12b3c25bdeba | 87 | } | 
| ptpaterson | 3:12b3c25bdeba | 88 | } | 
| ptpaterson | 3:12b3c25bdeba | 89 | |
| ptpaterson | 3:12b3c25bdeba | 90 | /* update all of the nodes */ | 
| ptpaterson | 2:c724ff3a4e4d | 91 | for (int i=0; i < nodeCount; i++) { | 
| ptpaterson | 3:12b3c25bdeba | 92 | nodes[i]->Update(); | 
| ptpaterson | 2:c724ff3a4e4d | 93 | } | 
| ptpaterson | 2:c724ff3a4e4d | 94 | } | 
| ptpaterson | 2:c724ff3a4e4d | 95 | } | 
| ptpaterson | 2:c724ff3a4e4d | 96 | |
| ptpaterson | 2:c724ff3a4e4d | 97 | /*============================================================================= | 
| ptpaterson | 2:c724ff3a4e4d | 98 | * Add Node to list | 
| ptpaterson | 2:c724ff3a4e4d | 99 | *============================================================================= | 
| ptpaterson | 2:c724ff3a4e4d | 100 | */ | 
| ptpaterson | 2:c724ff3a4e4d | 101 | |
| ptpaterson | 2:c724ff3a4e4d | 102 | int ServiceProvider::AddNode (Node * node) | 
| ptpaterson | 2:c724ff3a4e4d | 103 | { | 
| ptpaterson | 2:c724ff3a4e4d | 104 | int result = 1; | 
| ptpaterson | 2:c724ff3a4e4d | 105 | |
| ptpaterson | 2:c724ff3a4e4d | 106 | if (nodeCount < SERVICE_MAX_NODES) { | 
| ptpaterson | 2:c724ff3a4e4d | 107 | nodes[nodeCount++] = node; | 
| ptpaterson | 2:c724ff3a4e4d | 108 | } else { | 
| ptpaterson | 2:c724ff3a4e4d | 109 | result = 0; | 
| ptpaterson | 2:c724ff3a4e4d | 110 | } | 
| ptpaterson | 2:c724ff3a4e4d | 111 | |
| ptpaterson | 2:c724ff3a4e4d | 112 | return result; | 
| ptpaterson | 2:c724ff3a4e4d | 113 | } | 
| ptpaterson | 2:c724ff3a4e4d | 114 | |
| ptpaterson | 2:c724ff3a4e4d | 115 | /* ======================================================================== | 
| ptpaterson | 2:c724ff3a4e4d | 116 | * Methods to handle message requests and responses | 
| ptpaterson | 2:c724ff3a4e4d | 117 | * ======================================================================== | 
| ptpaterson | 2:c724ff3a4e4d | 118 | */ | 
| ptpaterson | 2:c724ff3a4e4d | 119 | |
| ptpaterson | 2:c724ff3a4e4d | 120 | void ServiceProvider::RequestPdo (const int pdoNum) | 
| ptpaterson | 2:c724ff3a4e4d | 121 | {} | 
| ptpaterson | 2:c724ff3a4e4d | 122 | |
| ptpaterson | 2:c724ff3a4e4d | 123 | void ServiceProvider::ProducePdo (const int pdoNum, char * data) | 
| ptpaterson | 2:c724ff3a4e4d | 124 | {} | 
| ptpaterson | 2:c724ff3a4e4d | 125 | |
| ptpaterson | 2:c724ff3a4e4d | 126 | int ServiceProvider::SendNodeControl (NmtCommandSpecifier cs, unsigned int targetId) | 
| ptpaterson | 2:c724ff3a4e4d | 127 | { | 
| ptpaterson | 2:c724ff3a4e4d | 128 | // CanOpenMessage canOpenMsg; | 
| ptpaterson | 2:c724ff3a4e4d | 129 | // | 
| ptpaterson | 2:c724ff3a4e4d | 130 | // canOpenMsg.data[0] = (char)cs; | 
| ptpaterson | 2:c724ff3a4e4d | 131 | // canOpenMsg.data[1] = (char)nodeId; | 
| ptpaterson | 2:c724ff3a4e4d | 132 | // canOpenMsg.dataCount = 2; | 
| ptpaterson | 2:c724ff3a4e4d | 133 | // canOpenMsg.type = CANOPEN_TYPE_DATA; | 
| ptpaterson | 2:c724ff3a4e4d | 134 | // canOpenMsg.format = CANOPEN_FORMAT_STANDARD; | 
| ptpaterson | 2:c724ff3a4e4d | 135 | // | 
| ptpaterson | 2:c724ff3a4e4d | 136 | // return CanOpenApiWrite (&canOpenMsg); | 
| ptpaterson | 2:c724ff3a4e4d | 137 | |
| ptpaterson | 2:c724ff3a4e4d | 138 | return 0; | 
| ptpaterson | 2:c724ff3a4e4d | 139 | } | 
| ptpaterson | 2:c724ff3a4e4d | 140 | |
| ptpaterson | 2:c724ff3a4e4d | 141 | int ServiceProvider::RequestErrorControl (NmtCommandSpecifier cs, unsigned int targetId) | 
| ptpaterson | 2:c724ff3a4e4d | 142 | { | 
| ptpaterson | 2:c724ff3a4e4d | 143 | //NMT_STATE_SET(Node::state.nmtState, NMT_STATE_UNKNOWN); | 
| ptpaterson | 2:c724ff3a4e4d | 144 | return 0; | 
| ptpaterson | 2:c724ff3a4e4d | 145 | } | 
| ptpaterson | 2:c724ff3a4e4d | 146 | |
| ptpaterson | 2:c724ff3a4e4d | 147 | int ServiceProvider::RespondErrorControl (NmtCommandSpecifier cs, unsigned int targetId) | 
| ptpaterson | 2:c724ff3a4e4d | 148 | { | 
| ptpaterson | 2:c724ff3a4e4d | 149 | //NMT_STATE_SET(Node::state.nmtState, NMT_STATE_UNKNOWN); | 
| ptpaterson | 2:c724ff3a4e4d | 150 | return 0; | 
| ptpaterson | 2:c724ff3a4e4d | 151 | } | 
| ptpaterson | 2:c724ff3a4e4d | 152 | |
| ptpaterson | 2:c724ff3a4e4d | 153 | |
| ptpaterson | 2:c724ff3a4e4d | 154 | } /* namspace ppCANOpen */ | 
| ptpaterson | 2:c724ff3a4e4d | 155 | |
| ptpaterson | 2:c724ff3a4e4d | 156 | |
| ptpaterson | 2:c724ff3a4e4d | 157 | |
| ptpaterson | 2:c724ff3a4e4d | 158 | |
| ptpaterson | 2:c724ff3a4e4d | 159 | |
| ptpaterson | 2:c724ff3a4e4d | 160 | |
| ptpaterson | 2:c724ff3a4e4d | 161 |