examples/CANOpenShell/CANOpenShell.c
changeset 559 8543e7edae4a
parent 555 ee24dcbd3e64
child 568 84fb13d1b3d2
--- a/examples/CANOpenShell/CANOpenShell.c	Wed Jul 15 17:14:44 2009 +0200
+++ b/examples/CANOpenShell/CANOpenShell.c	Wed Jul 15 17:17:47 2009 +0200
@@ -30,7 +30,6 @@
 	#include <stdio.h>
 	#include <string.h>
 	#include <stdlib.h>
-	#include <signal.h>
 	#define CLEARSCREEN "clear"
 	#define SLEEP(time) sleep(time)
 #endif
@@ -59,20 +58,9 @@
 char BoardBusName[11];
 char BoardBaudRate[5];
 s_BOARD Board = {BoardBusName, BoardBaudRate};
-CO_Data* CANOpenShellOD_Data;
-int init_step = 0;
+CO_Data* CANOpenShellOD_Data;
 char LibraryPath[512];
 
-/*****************************************************************************/
-#if !defined(WIN32) || defined(__CYGWIN__)
-void catch_signal(int sig)
-{
-	signal(SIGTERM, catch_signal);
-	signal(SIGINT, catch_signal);
-	printf("Got Signal %d\n",sig);
-}
-#endif
-
 /* Sleep for n seconds */
 void SleepFunction(int second)
 {
@@ -80,36 +68,31 @@
 }
 
 /* Ask a slave node to go in operational mode */
-void StartNode(CO_Data* d, UNS8 nodeid)
-{
-	EnterMutex();
-	masterSendNMTstateChange(d, nodeid, NMT_Start_Node);
-	LeaveMutex();
+void StartNode(UNS8 nodeid)
+{
+	masterSendNMTstateChange(CANOpenShellOD_Data, nodeid, NMT_Start_Node);
 }
 
 /* Ask a slave node to go in pre-operational mode */
-void StopNode(CO_Data* d, UNS8 nodeid)
-{
-	EnterMutex();
-	masterSendNMTstateChange(d, nodeid, NMT_Stop_Node);
-	LeaveMutex();
+void StopNode(UNS8 nodeid)
+{
+	masterSendNMTstateChange(CANOpenShellOD_Data, nodeid, NMT_Stop_Node);
 }
 
 /* Ask a slave node to reset */
-void ResetNode(CO_Data* d, UNS8 nodeid)
-{
-	EnterMutex();
-	masterSendNMTstateChange(d, nodeid, NMT_Reset_Node);
-	LeaveMutex();
+void ResetNode(UNS8 nodeid)
+{
+	masterSendNMTstateChange(CANOpenShellOD_Data, nodeid, NMT_Reset_Node);
 }
 
 /* Reset all nodes on the network and print message when boot-up*/
-void DiscoverNodes(CO_Data* d)
+void DiscoverNodes()
 {
 	printf("Wait for Slave nodes bootup...\n\n");
-	ResetNode(CANOpenShellOD_Data, 0x0);
-}
-
+	ResetNode(0x00);
+}
+
+int get_info_step = 0;
 /* Callback function that check the read SDO demand */
 void CheckReadInfoSDO(CO_Data* d, UNS8 nodeid)
 {
@@ -117,14 +100,14 @@
 	UNS32 data;
 	UNS32 size=64;
 
-	if(getReadResultNetworkDict(d, nodeid, &data, &size, &abortCode) != SDO_FINISHED)
+	if(getReadResultNetworkDict(CANOpenShellOD_Data, nodeid, &data, &size, &abortCode) != SDO_FINISHED)
 		printf("Master : Failed in getting information for slave %2.2x, AbortCode :%4.4x \n", nodeid, abortCode);
 
-	/* Finalise last SDO transfer with this node */
+	/* Finalize last SDO transfer with this node */
 	closeSDOtransfer(CANOpenShellOD_Data, nodeid, SDO_CLIENT);
 
 	/* Display data received */
-	switch(init_step)
+	switch(get_info_step)
 	{
 		case 1:
 				printf("Device type     : %x\n", data);
@@ -139,35 +122,35 @@
 				printf("Revision Number : %x\n", data);
 				break;
 	}
-	GetSlaveNodeInfo(d, nodeid);
+	GetSlaveNodeInfo(nodeid);
 }
 
 /* Retrieve node informations located at index 0x1000 (Device Type) and 0x1018 (Identity) */
-void GetSlaveNodeInfo(CO_Data* d, UNS8 nodeid)
-{
-		switch(++init_step)
+void GetSlaveNodeInfo(UNS8 nodeid)
+{
+		switch(++get_info_step)
 		{
 			case 1: /* Get device type */
 				printf("##################################\n");
 				printf("#### Informations for node %x ####\n", nodeid);
 				printf("##################################\n");
-				readNetworkDictCallback(d, nodeid, 0x1000, 0x00, 0, CheckReadInfoSDO);
+				readNetworkDictCallback(CANOpenShellOD_Data, nodeid, 0x1000, 0x00, 0, CheckReadInfoSDO);
 				break;
 
 			case 2: /* Get Vendor ID */
-				readNetworkDictCallback(d, nodeid, 0x1018, 0x01, 0, CheckReadInfoSDO);
+				readNetworkDictCallback(CANOpenShellOD_Data, nodeid, 0x1018, 0x01, 0, CheckReadInfoSDO);
 				break;
 
 			case 3: /* Get Product Code */
-				readNetworkDictCallback(d, nodeid, 0x1018, 0x02, 0, CheckReadInfoSDO);
+				readNetworkDictCallback(CANOpenShellOD_Data, nodeid, 0x1018, 0x02, 0, CheckReadInfoSDO);
 				break;
 
 			case 4: /* Get Revision Number */
-				readNetworkDictCallback(d, nodeid, 0x1018, 0x03, 0, CheckReadInfoSDO);
+				readNetworkDictCallback(CANOpenShellOD_Data, nodeid, 0x1018, 0x03, 0, CheckReadInfoSDO);
 				break;
 
 			case 5: /* Print node info */
-				init_step = 0;
+				get_info_step = 0;
 		}
 }
 
@@ -178,34 +161,39 @@
 	UNS32 data;
 	UNS32 size=64;
 
-	if(getReadResultNetworkDict(d, nodeid, &data, &size, &abortCode) != SDO_FINISHED)
+	if(getReadResultNetworkDict(CANOpenShellOD_Data, nodeid, &data, &size, &abortCode) != SDO_FINISHED)
 		printf("\nResult : Failed in getting information for slave %2.2x, AbortCode :%4.4x \n", nodeid, abortCode);
 	else
-		printf("\nResult : = %x\n", data);
-
-	/* Finalise last SDO transfer with this node */
+		printf("\nResult : %x\n", data);
+
+	/* Finalize last SDO transfer with this node */
 	closeSDOtransfer(CANOpenShellOD_Data, nodeid, SDO_CLIENT);
 }
 
 /* Read a slave node object dictionary entry */
-void ReadDeviceEntry(CO_Data* d, char* sdo)
-{
+void ReadDeviceEntry(char* sdo)
+{
+	int ret=0;
 	int nodeid;
 	int index;
 	int subindex;
-	int size;
 	int datatype = 0;
 
-	sscanf(sdo, "%2x%4x%2x", &nodeid, &index, &subindex);
-
-	printf("##################################\n");
-	printf("#### Read SDO                 ####\n");
-	printf("##################################\n");
-	printf("NodeId   : %2.2x\n", nodeid);
-	printf("Index    : %4.4x\n", index);
-	printf("SubIndex : %2.2x\n", subindex);
-
-	readNetworkDictCallback(d, (UNS8)nodeid, (UNS16)index, (UNS8)subindex, (UNS8)datatype, CheckReadSDO);
+	ret = sscanf(sdo, "rsdo#%2x,%4x,%2x", &nodeid, &index, &subindex);
+	if (ret == 3)
+	{
+
+		printf("##################################\n");
+		printf("#### Read SDO                 ####\n");
+		printf("##################################\n");
+		printf("NodeId   : %2.2x\n", nodeid);
+		printf("Index    : %4.4x\n", index);
+		printf("SubIndex : %2.2x\n", subindex);
+
+		readNetworkDictCallback(CANOpenShellOD_Data, (UNS8)nodeid, (UNS16)index, (UNS8)subindex, (UNS8)datatype, CheckReadSDO);
+	}
+	else
+		printf("Wrong command  : %s\n", sdo);
 }
 
 /* Callback function that check the write SDO demand */
@@ -213,36 +201,41 @@
 {
 	UNS32 abortCode;
 
-	if(getWriteResultNetworkDict(d, nodeid, &abortCode) != SDO_FINISHED)
+	if(getWriteResultNetworkDict(CANOpenShellOD_Data, nodeid, &abortCode) != SDO_FINISHED)
 		printf("\nResult : Failed in getting information for slave %2.2x, AbortCode :%4.4x \n", nodeid, abortCode);
 	else
 		printf("\nSend data OK\n");
 
-	/* Finalise last SDO transfer with this node */
+	/* Finalize last SDO transfer with this node */
 	closeSDOtransfer(CANOpenShellOD_Data, nodeid, SDO_CLIENT);
 }
 
 /* Write a slave node object dictionnary entry */
-void WriteDeviceEntry(CO_Data* d, char* sdo)
-{
+void WriteDeviceEntry(char* sdo)
+{
+	int ret=0;
 	int nodeid;
 	int index;
 	int subindex;
 	int size;
 	int data;
 
-	sscanf(sdo, "%2x%4x%2x%2x%4x", &nodeid , &index, &subindex, &size, &data);
-
-	printf("##################################\n");
-	printf("#### Write SDO                ####\n");
-	printf("##################################\n");
-	printf("NodeId   : %2.2x\n", nodeid);
-	printf("Index    : %4.4x\n", index);
-	printf("SubIndex : %2.2x\n", subindex);
-	printf("Size     : %2.2x\n", size);
-	printf("Data     : %x\n", data);
-
-	writeNetworkDictCallBackAI(d, nodeid, index, subindex, size, 0, &data, CheckWriteSDO, 1);
+	ret = sscanf(sdo, "wsdo#%2x,%4x,%2x,%2x,%x", &nodeid , &index, &subindex, &size, &data);
+	if (ret == 5)
+	{
+		printf("##################################\n");
+		printf("#### Write SDO                ####\n");
+		printf("##################################\n");
+		printf("NodeId   : %2.2x\n", nodeid);
+		printf("Index    : %4.4x\n", index);
+		printf("SubIndex : %2.2x\n", subindex);
+		printf("Size     : %2.2x\n", size);
+		printf("Data     : %x\n", data);
+
+		writeNetworkDictCallBack(CANOpenShellOD_Data, nodeid, index, subindex, size, 0, &data, CheckWriteSDO);
+	}
+	else
+		printf("Wrong command  : %s\n", sdo);
 }
 
 void CANOpenShellOD_post_SlaveBootup(CO_Data* d, UNS8 nodeid)
@@ -253,22 +246,22 @@
 /***************************  CALLBACK FUNCTIONS  *****************************************/
 void CANOpenShellOD_initialisation(CO_Data* d)
 {
-	//printf("Master_initialisation\n");
+	printf("Node_initialisation\n");
 }
 
 void CANOpenShellOD_preOperational(CO_Data* d)
 {
-	//printf("Master_preOperational\n");
+	printf("Node_preOperational\n");
 }
 
 void CANOpenShellOD_operational(CO_Data* d)
 {
-	//printf("Master_operational\n");
+	printf("Node_operational\n");
 }
 
 void CANOpenShellOD_stopped(CO_Data* d)
 {
-	//printf("Master_stopped\n");
+	printf("Node_stopped\n");
 }
 
 void CANOpenShellOD_post_sync(CO_Data* d)
@@ -281,41 +274,32 @@
 	//printf("Master_post_TPDO\n");
 }
 
-/***************************  MASTER INITIALISATION **********************************/
-void Init(CO_Data* d, UNS32 nodeid)
+/***************************  INITIALISATION  **********************************/
+void Init(CO_Data* d, UNS32 id)
 {
 	if(Board.baudrate)
 	{
-		/* Defining the node Id */
-		setNodeId(CANOpenShellOD_Data, nodeid);
-
-		/* Init */
+		/* Init node state*/
 		setState(CANOpenShellOD_Data, Initialisation);
 	}
 }
 
-/***************************  MASTER CLEANUP  *****************************************/
+/***************************  CLEANUP  *****************************************/
 void Exit(CO_Data* d, UNS32 nodeid)
 {
 	if(strcmp(Board.baudrate, "none"))
 	{
 		/* Reset all nodes on the network */
-		masterSendNMTstateChange(CANOpenShellOD_Data, (UNS8)nodeid, NMT_Reset_Node);
+		masterSendNMTstateChange(CANOpenShellOD_Data, 0 , NMT_Reset_Node);
 
 		/* Stop master */
 		setState(CANOpenShellOD_Data, Stopped);
 	}
 }
 
-int ExtractNodeId(char *command) {
-	int nodeid;
-	sscanf(command, "%2x", &nodeid);
-	return nodeid;
-}
-
-int NodeInit(CO_Data* d, int DeviceNodeID, int DeviceIsMaster)
-{
-	if(DeviceIsMaster)
+int NodeInit(int NodeID, int NodeType)
+{
+	if(NodeType)
 	{
 		CANOpenShellOD_Data = &CANOpenShellMasterOD_Data;
 	}
@@ -327,9 +311,6 @@
 	/* Load can library */
 	LoadCanDriver(LibraryPath);
 
-	/* Init stack timer */
-	TimerInit();
-
 	/* Define callback functions */
 	CANOpenShellOD_Data->initialisation = CANOpenShellOD_initialisation;
 	CANOpenShellOD_Data->preOperational = CANOpenShellOD_preOperational;
@@ -340,8 +321,10 @@
 	CANOpenShellOD_Data->post_SlaveBootup=CANOpenShellOD_post_SlaveBootup;
 
 	/* Open the Peak CANOpen device */
-	if(!canOpen(&Board,CANOpenShellOD_Data)) return 1;
-
+	if(!canOpen(&Board,CANOpenShellOD_Data)) return INIT_ERR;
+
+	/* Defining the node Id */
+	setNodeId(CANOpenShellOD_Data, NodeID);
 	/* Start Timer thread */
 	StartTimerLoop(&Init);
 	return 0;
@@ -350,19 +333,23 @@
 void help_menu(void)
 {
 	printf("   MANDATORY COMMAND (must be the first command):\n");
-	printf("      load#Can library path,channel,baudrate,device nodeid,device type (m for master, s for slave)\n");
+	printf("     load#CanLibraryPath,channel,baudrate,nodeid,type (0:slave, 1:master)\n");
 	printf("\n");
 	printf("   NETWORK: (if nodeid=0x00 : broadcast)\n");
-	printf("     ssta#[nodeid] : Start a node\n");
-	printf("     ssto#[nodeid] : Stop a node\n");
-	printf("     srst#[nodeid] : Reset a node\n");
+	printf("     ssta#nodeid : Start a node\n");
+	printf("     ssto#nodeid : Stop a node\n");
+	printf("     srst#nodeid : Reset a node\n");
 	printf("     scan : Reset all nodes and print message when bootup\n");
-	printf("     wait#[seconds] : Sleep for n seconds\n");
-	printf("\n");
-	printf("   SDO: (size parameter : nb BYTES)\n");
-	printf("     info#[nodeid]\n");
-	printf("     rsdo#[nodeid][index][subindex] : read sdo\n");
-	printf("     wsdo#[nodeid][index][subindex][size][data] : write sdo\n");
+	printf("     wait#seconds : Sleep for n seconds\n");
+	printf("\n");
+	printf("   SDO: (size in bytes)\n");
+	printf("     info#nodeid\n");
+	printf("     rsdo#nodeid,index,subindex : read sdo\n");
+	printf("        ex : rsdo#42,1018,01\n");
+	printf("     wsdo#nodeid,index,subindex,size,data : write sdo\n");
+	printf("        ex : wsdo#42,6200,01,01,FF\n");
+	printf("\n");
+	printf("   Note: All numbers are hex\n");
 	printf("\n");
 	printf("     help : Display this menu\n");
 	printf("     quit : Quit application\n");
@@ -370,66 +357,76 @@
 	printf("\n");
 }
 
+int ExtractNodeId(char *command) {
+	int nodeid;
+	sscanf(command, "%2x", &nodeid);
+	return nodeid;
+}
+
 int ProcessCommand(char* command)
 {
 	int ret = 0;
 	int sec = 0;
-	int DeviceNodeID;
-	int DeviceType;
-
+	int NodeID;
+	int NodeType;
+
+	EnterMutex();
 	switch(cst_str4(command[0], command[1], command[2], command[3]))
 	{
+		case cst_str4('h', 'e', 'l', 'p') : /* Display Help*/
+					help_menu();
+					break;
+		case cst_str4('s', 's', 't', 'a') : /* Slave Start*/
+					StartNode(ExtractNodeId(command + 5));
+					break;
+		case cst_str4('s', 's', 't', 'o') : /* Slave Stop */
+					StopNode(ExtractNodeId(command + 5));
+					break;
+		case cst_str4('s', 'r', 's', 't') : /* Slave Reset */
+					ResetNode(ExtractNodeId(command + 5));
+					break;
+		case cst_str4('i', 'n', 'f', 'o') : /* Retrieve node informations */
+					GetSlaveNodeInfo(ExtractNodeId(command + 5));
+					break;
+		case cst_str4('r', 's', 'd', 'o') : /* Read device entry */
+					ReadDeviceEntry(command);
+					break;
+		case cst_str4('w', 's', 'd', 'o') : /* Write device entry */
+					WriteDeviceEntry(command);
+					break;
+		case cst_str4('s', 'c', 'a', 'n') : /* Display master node state */
+					DiscoverNodes();
+					break;
+		case cst_str4('w', 'a', 'i', 't') : /* Display master node state */
+					ret = sscanf(command, "wait#%d", &sec);
+					if(ret == 1) {
+						LeaveMutex();
+						SleepFunction(sec);
+						return 0;
+					}
+					break;
+		case cst_str4('q', 'u', 'i', 't') : /* Quit application */
+					LeaveMutex();
+					return QUIT;
 		case cst_str4('l', 'o', 'a', 'd') : /* Library Interface*/
 					ret = sscanf(command, "load#%100[^,],%10[^,],%4[^,],%d,%d",
 							LibraryPath,
 							BoardBusName,
 							BoardBaudRate,
-							&DeviceNodeID,
-							&DeviceType);
+							&NodeID,
+							&NodeType);
 
 					if(ret == 5)
 					{
-						ret = NodeInit(CANOpenShellOD_Data, DeviceNodeID, DeviceType);
+						ret = NodeInit(NodeID, NodeType);
+						LeaveMutex();
 						return ret;
 					}
-					else{
-						help_menu();
-						exit(1);
-					}
-					break;
-		case cst_str4('h', 'e', 'l', 'p') : /* Display Help*/
-					help_menu();
-					break;
-		case cst_str4('s', 's', 't', 'a') : /* Slave Start*/
-					StartNode(CANOpenShellOD_Data, ExtractNodeId(command + 5));
-					break;
-		case cst_str4('s', 's', 't', 'o') : /* Slave Stop */
-					StopNode(CANOpenShellOD_Data, ExtractNodeId(command + 5));
-					break;
-		case cst_str4('s', 'r', 's', 't') : /* Slave Reset */
-					ResetNode(CANOpenShellOD_Data, ExtractNodeId(command + 5));
-					break;
-		case cst_str4('i', 'n', 'f', 'o') : /* Retrieve node informations */
-					GetSlaveNodeInfo(CANOpenShellOD_Data, ExtractNodeId(command + 5));
-					break;
-		case cst_str4('r', 's', 'd', 'o') : /* Read device entry */
-					ReadDeviceEntry(CANOpenShellOD_Data, command + 5);
-					break;
-		case cst_str4('w', 's', 'd', 'o') : /* Write device entry */
-					WriteDeviceEntry(CANOpenShellOD_Data, command + 5);
-					break;
-		case cst_str4('s', 'c', 'a', 'n') : /* Display master node state */
-					DiscoverNodes(CANOpenShellOD_Data);
-					break;
-		case cst_str4('w', 'a', 'i', 't') : /* Display master node state */
-					ret = sscanf(command, "wait=%d", &sec);
-					if(ret == 1) SleepFunction(sec);
-					break;
-		case cst_str4('q', 'u', 'i', 't') : /* Quit application */
-					return QUIT;
+					break;
 		default :
 					help_menu();
 	}
+	LeaveMutex();
 	return 0;
 }
 
@@ -440,31 +437,28 @@
 int main(int argc, char** argv)
 {
 	extern char *optarg;
-	char command[20];
+	char command[200];
 	char* res;
 	int ret=0;
 	int sysret=0;
 	int i=0;
 
 	/* Print help and exit immediatly*/
-	if(argc < 1)
+	if(argc < 2)
 	{
 		help_menu();
 		exit(1);
 	}
 
+	/* Init stack timer */
+	TimerInit();
+
 	/* Strip command-line*/
 	for(i=1 ; i<argc ; i++)
 	{
 		if(ProcessCommand(argv[i]) == INIT_ERR) goto init_fail;
 	}
 
-	#if !defined(WIN32) || defined(__CYGWIN__)
-		/* install signal handler for manual break */
-		signal(SIGTERM, catch_signal);
-		signal(SIGINT, catch_signal);
-	#endif
-
 	/* Enter in a loop to read stdin command until "quit" is called */
 	while(ret != QUIT)
 	{