Access Control is now enabled for Gitlab Pages so you can now restrict Pages to authorized users only. If needed, make your website publicly available : Navigate to your project's Settings > General > Visibility and select Everyone in pages section.

Add some comments for a better comprehension of the code

parent e336543b
......@@ -22,7 +22,6 @@
* Author: Davide Previte / HEIA-FR
* Date: 15.01.2019
*
*
*/
#include "rfid.h"
......@@ -31,9 +30,13 @@
#include <sys/types.h>
#include <sys/stat.h>
char uidS[MAX_UID];
char uidS[MAX_UID]; //char array that should contains the UID
//open the reader with termios structure
/**
* open the reader with termios structure
* @param serial is the location of the reader ("/dev/ttyACM0" by default)
* @return fd is the file desciptor of the RFID reader
*/
int openSerial(const char *serial)
{
int fd = open(serial, O_RDWR | O_NOCTTY | O_NDELAY);
......@@ -73,25 +76,22 @@ int openSerial(const char *serial)
return fd;
}
//read the uid and return it
/**
* read the uid and return it
* @param fd is the file descriptor of the RFID reader
* @return uidS char[] with the UID read by the RFID reader
*/
char* readUID(int fd)
{
unsigned nbBytes = 0;
int bytes_to_read = 0;
unsigned nbBytes = 0; //number of bytes read
char x; //char that is read by the reader
char x;
//tant que les caractères sont vides (\n ou \r) lire
/*while(1){
read(fd, &x, 1);
if(strlen(x) >= 1) {
printf("Caractère non-nul \n");
break;
}
}*/
read(fd, &x, 1);
while(x == '\0' || x == '\n' && x == '\r')read(fd, &x, 1);
//if the char read is one of this, nothing to do
while(x == '\0' || x == '\n' && x == '\r') read(fd, &x, 1);
//read the UID while there is a char
while(x != '\n' && x != '\r'){
uidS[nbBytes++] = x;
if (nbBytes >= sizeof(uidS)) {
......@@ -101,6 +101,7 @@ char* readUID(int fd)
read(fd, &x, 1);
}
//the last char should be a 0 because it is a string
uidS[nbBytes] = 0;
return uidS;
}
......
#pragma once
#ifndef RFID_H
#define RFID_H
/**
* Copyright 2018 University of Applied Sciences Western Switzerland / Fribourg
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project: HEIA-FR / Project 5
*
* Abstract: Badge RFID connected to the iIoT
*
* Purpose: Header for reading the UID from the TWN4 reader
*
* Author: Davide Previte / HEIA-FR
* Date: 15.01.2019
*/
#include <errno.h>
#include <fcntl.h>
#include <string.h>
......@@ -11,13 +39,23 @@
#include <sys/types.h>
#include <sys/stat.h>
//Warning : must be bi enough !!
//Warning : must be big enough !!
#define MAX_UID 50
//open the reader with termios structure
/**
* open the reader with termios structure
* @param serial is the location of the reader ("/dev/ttyACM0" by default)
* @return fd is the file desciptor of the RFID reader
*/
int openSerial(const char *serial);
//read the uid and return it
/**
* read the uid and return it
* @param fd is the file descriptor of the RFID reader
* @return uidS char[] with the UID read by the RFID reader
*/
char* readUID(int fd);
#endif
#!bin/sh
#do all the update and upgrade to start with a clean installation
sudo apt update -y && sudo apt upgrade -y && sudo apt dist-upgrade -y
echo "Installing rfid reader..."
#copy all the file in the home directory
cp -r ../RFID $HOME
#go to the home directory
cd $HOME/RFID
#copy the rfid service into the systemd directory (should be sudo)
sudo cp rfid.service /etc/systemd/system/
#clean the project
make clean
#make the project
make
#enable the rfid service (should be done after making the project)
echo "Enabling rfid service"
sudo systemctl enable rfid
echo "Ok"
......
......@@ -31,7 +31,6 @@
*
*/
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
......@@ -41,9 +40,6 @@
#include "open62541.h"
#include "server_rfid.h"
#include "rfid.h"
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
//Define the sampling time for the reader
#define SLEEP_TIME_MILLIS 50
......@@ -52,23 +48,28 @@
#define UID_NODE_ID 20305
#define CHECK_NODE_ID 21305
UA_String myUID;
UA_Int32 myInteger;
UA_UInt32 myChecked;
UA_String myUID; //variable that contains the uid for the OPC UA server
UA_Server *server;
UA_Server *server; //it is the OPC UA server
//UA_String_init(&myUID); /* _init zeroes out the entire memory of the datatype */
/**
* Method that stop the server if ctrl-c is catch
* @param sig
*/
static void stopHandler(int sig)
{
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Received ctrl-c");
running = false;
}
/**
* Method that send the UID on the OPC UA server
* @param uid is a String with the UID
*/
void sendUID(char *uid)
{
//printf("Card detected: %s\n", uid);
UA_Variant value;
myUID.length = strlen(uid);
myUID.data = (UA_Byte *)uid;
......@@ -76,43 +77,48 @@ void sendUID(char *uid)
UA_Server_writeValue(server, UA_NODEID_NUMERIC(1, UID_NODE_ID), value);
}
//Thread used to monitor the RFID reader
/**
* Thread used to monitorize the sensor
* @param ptr is the pointer of the server OPC UA
*/
void *mainReader(void *ptr)
{
server = ptr;
int utime = SLEEP_TIME_MILLIS * 10;
char uid[MAX_UID];
char uid_old[MAX_UID];
char msg[MAX_UID];
int nbCheck = 0;
server = ptr; //the pointer is the server
int utime = SLEEP_TIME_MILLIS * 10; //time to sleep before sending next uid
char uid[MAX_UID]; //current uid read from the RFID reader
char uid_old[MAX_UID]; //last uid read from the RFID reader
int nbCheck = 0; //number of time that the same uid is read
char msg[MAX_UID]; //it is the String sended to the server that contains the UID and the number of time that it was read
int fd = openSerial("/dev/ttyACM0");
int fd = openSerial("/dev/ttyACM0"); //file descriptor of the RFID reader
while (running == 1)
{
char *uid = readUID(fd);
char *uid = readUID(fd); //UID read from the RFID reader
//si UID identique, incremente nbCheck
//if the uid is the same that last uid, we should increment nbCheck
if (strcmp(uid, uid_old) == 0)
{
//printf("Deux fois la meme carte\n");
nbCheck= nbCheck+1;
snprintf(msg, MAX_UID, "%s ; %d", uid, nbCheck);
nbCheck += 1;
snprintf(msg, MAX_UID, "%s ; %d", uid, nbCheck); //create the String for the server
sendUID(msg);
}else{
//printf("New card %s\n", uid);
}
//else it is a new uid, so nbCheck is 1
else{
nbCheck = 1;
snprintf(msg, MAX_UID, "%s ; %d", uid, nbCheck);
snprintf(msg, MAX_UID, "%s ; %d", uid, nbCheck); //create the String for the server
sendUID(msg);
strcpy(uid_old, uid);
}
usleep(utime);
}
}
/**
* Method needed to add the UID variable on the server OPC UA
* @param server is the pointer of the OPC UA server
*/
static void addUidVariable(UA_Server *server)
{
UA_NodeId uidNodeId = UA_NODEID_NUMERIC(1, UID_NODE_ID);
......@@ -121,7 +127,6 @@ static void addUidVariable(UA_Server *server)
attr.description = UA_LOCALIZEDTEXT("en_US", "UID number");
attr.displayName = UA_LOCALIZEDTEXT("en_US", "UID");
attr.dataType = UA_TYPES[UA_TYPES_STRING].typeId;
//UA_String myUID;
UA_Variant_setScalarCopy(&attr.value, &myUID, &UA_TYPES[UA_TYPES_STRING]);
UA_Server_addVariableNode(server, uidNodeId,
UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
......@@ -129,42 +134,10 @@ static void addUidVariable(UA_Server *server)
uidName, UA_NODEID_NULL, attr, NULL, NULL);
}
/*
static void addCheckVariable(UA_Server *server)
{
UA_NodeId NodeId = UA_NODEID_NUMERIC(1, CHECK_NODE_ID);
UA_QualifiedName Name = UA_QUALIFIEDNAME(1, "Checked number of UID");
UA_VariableAttributes attr = UA_VariableAttributes_default;
attr.description = UA_LOCALIZEDTEXT("en_US", "Checked number");
attr.displayName = UA_LOCALIZEDTEXT("en_US", "Checked");
attr.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
//UA_String myUID;
UA_Variant_setScalarCopy(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
UA_Server_addVariableNode(server, NodeId,
UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
Name, UA_NODEID_NULL, attr, NULL, NULL);
/* Define the attribute of the myInteger variable node
UA_VariableAttributes attr = UA_VariableAttributes_default;
myInteger = 0;
UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
attr.description = UA_LOCALIZEDTEXT("en_US", "Checked number");
attr.displayName = UA_LOCALIZEDTEXT("en_US", "Checked");
attr.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
Add the variable node to the information model
UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "Checked number of UID");
UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "Number of time");
UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, CHECK_NODE_ID);
UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
parentReferenceNodeId, myIntegerName,
UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), attr, NULL, NULL);
}
*/
/**
* Main programm that launch the server OPC UA and add all the variable. Then it creates a thread that read the uid of the RFID reader
* and send that to the server
*/
int main(void)
{
int ret;
......@@ -175,15 +148,14 @@ int main(void)
UA_Server *server = UA_Server_new(config);
addUidVariable(server);
//addCheckVariable(server);
//Launch the thread. The OPC-UA server is passed as parameter as the value of the node needs to be updated.
//Launch the thread. The OPC-UA server is passed as parameter because the value of the node needs to be updated.
if (pthread_create(&threadReader, NULL, mainReader, server))
{
fprintf(stderr, "Error - pthread_create(): %d\n", ret);
exit(EXIT_FAILURE);
}
UA_StatusCode retval = UA_Server_run(server, &running);
UA_StatusCode retval = UA_Server_run(server, &running); //the programm remains blocked here while any signal is catched
UA_Server_delete(server);
UA_ServerConfig_delete(config);
return (int)retval;
......
//gcc -std=c99 -DUA_ARCHITECTURE_POSIX open62541.c server_rfid.c -o server -lpthread
#pragma once
#ifndef SERVER_RFID_H
#define SERVER_RFID_H
/**
* Copyright 2018 University of Applied Sciences Western Switzerland / Fribourg
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project: HEIA-FR / Project 5
*
* Abstract: Badge RFID connected to the iIoT
*
* Purpose: OPC UA server that send the UID read from the TWN4 to the client OPC UA
*
* Author: Davide Previte / HEIA-FR
* Date: 15.01.2019
*
*
* Site where to find how to change the variable in the opc ua server
* https://medium.com/gradiant-talks/implementing-an-opc-ua-server-using-open62541-77aeddd99370
*
* Site where to find how to change the hostname
* https://github.com/open62541/open62541/issues/535
*
*/
#include <stdio.h>
#include <stdlib.h>
......@@ -15,17 +49,36 @@
//Define the ID of the node externally as it will be needed inside the thread
#define COUNTER_NODE_ID 20305
UA_Boolean running = true;
UA_Boolean running = true; // Boolean that show if the server is running or not
/**
* Method that stop the server if ctrl-c is catch
* @param sig
*/
static void stopHandler(int sig);
/**
* Method that send the UID on the OPC UA server
* @param uid is a String with the UID
*/
void sendUID(char* uid);
//Thread used to monitorize the sensor
/**
* Thread used to monitorize the sensor
* @param ptr is the pointer of the server OPC UA
*/
void *mainSensor(void *ptr);
/**
* Method needed to add the UID variable on the server OPC UA
* @param server is the pointer of the OPC UA server
*/
static void addUidVariable(UA_Server *server);
static void addCheckVariable(UA_Server *server);
/**
* Main programm that launch the server OPC UA and add all the variable. Then it creates a thread that read the uid of the RFID reader
* and send that to the server
*/
int main(void);
int main(void);
\ No newline at end of file
#endif
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment