Skip to content

Commit e22264f

Browse files
committed
Merge pull request #361 from BernardXiong/master
Add cplusplus and sensor framework.
2 parents a995264 + b081df6 commit e22264f

File tree

9 files changed

+1245
-10
lines changed

9 files changed

+1245
-10
lines changed

components/cplusplus/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# C++ support for RT-Thread #
2+
3+
This is the C++ component in RT-Thread RTOS. In order to support C++ language, this component
4+
implement a basic environment, such as new/delete operators.
5+
6+
Because RT-Thread RTOS is used in embedded system mostly, there are some rules for C++ applications:
7+
1. DOES NOT use exception.
8+
2. DOES NOT use Run-Time Type Information (RTTI).
9+
3. Template is discouraged and it easily causes code text large.
10+
4. Static class variables are discouraged. The time and place to call their constructor function could not be precisely controlled and make multi-threaded programming a nightmare.
11+
5. Multiple inheritance is strongly discouraged, as it can cause intolerable confusion.
12+
13+
*NOTE*: For armcc compiler, the libc must be enable.

components/cplusplus/SConscript

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# RT-Thread building script for component
2+
3+
from building import *
4+
5+
cwd = GetCurrentDir()
6+
src = Glob('*.cpp')
7+
CPPPATH = [cwd]
8+
9+
group = DefineGroup('CPlusPlus', src, depend = ['RT_USING_CPLUSPLUS'], CPPPATH = CPPPATH)
10+
11+
Return('group')

components/cplusplus/crt.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <rtthread.h>
2+
#include "crt.h"
3+
4+
void *operator new(size_t size)
5+
{
6+
return rt_malloc(size);
7+
}
8+
9+
void *operator new[](size_t size)
10+
{
11+
return rt_malloc(size);
12+
}
13+
14+
void operator delete(void *ptr)
15+
{
16+
rt_free(ptr);
17+
}
18+
19+
void operator delete[] (void *ptr)
20+
{
21+
return rt_free(ptr);
22+
}
23+
24+
void __cxa_pure_virtual(void)
25+
{
26+
rt_kprintf("Illegal to call a pure virtual function.\n");
27+
}

components/cplusplus/crt.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef CRT_H_
2+
#define CRT_H_
3+
4+
#include <inttypes.h>
5+
#include <stdlib.h>
6+
7+
void *operator new(size_t size);
8+
void *operator new[](size_t size);
9+
10+
void operator delete(void * ptr);
11+
void operator delete[] (void *ptr);
12+
13+
extern "C" void __cxa_pure_virtual(void);
14+
15+
#endif

components/dfs/filesystems/nfs/dfs_nfs.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -750,11 +750,12 @@ int nfs_open(struct dfs_fd *file)
750750
if (file->flags & DFS_O_CREAT)
751751
{
752752
if (nfs_mkdir(nfs, file->path, 0755) < 0)
753-
return -1;
753+
return -DFS_STATUS_EAGAIN;
754754
}
755755

756756
/* open directory */
757757
dir = nfs_opendir(nfs, file->path);
758+
if (dir == RT_NULL) return -DFS_STATUS_ENOENT;
758759
file->data = dir;
759760
}
760761
else
@@ -766,20 +767,20 @@ int nfs_open(struct dfs_fd *file)
766767
if (file->flags & DFS_O_CREAT)
767768
{
768769
if (nfs_create(nfs, file->path, 0664) < 0)
769-
return -1;
770+
return -DFS_STATUS_EAGAIN;
770771
}
771772

772773
/* open file (get file handle ) */
773774
fp = rt_malloc(sizeof(nfs_file));
774775
if (fp == RT_NULL)
775-
return -1;
776+
return -DFS_STATUS_ENOMEM;
776777

777778
handle = get_handle(nfs, file->path);
778779
if (handle == RT_NULL)
779780
{
780781
rt_free(fp);
781782

782-
return -1;
783+
return -DFS_STATUS_ENOENT;
783784
}
784785

785786
/* get size of file */
@@ -798,7 +799,7 @@ int nfs_open(struct dfs_fd *file)
798799

799800
/* set private file */
800801
file->data = fp;
801-
file->size = fp->size;
802+
file->size = fp->size;
802803
}
803804

804805
return 0;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# SConscript for sensor framework
2+
3+
from building import *
4+
5+
cwd = GetCurrentDir()
6+
src = Glob('*.c') + Glob('*.cpp')
7+
CPPPATH = [cwd, cwd + '/../include']
8+
9+
group = DefineGroup('Sensors', src, depend = ['RT_USING_SENSOR', 'RT_USING_DEVICE'], CPPPATH = CPPPATH)
10+
11+
Return('group')
12+
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
#include <stddef.h>
2+
#include "sensor.h"
3+
4+
/**
5+
* Sensor
6+
*/
7+
Sensor::Sensor()
8+
{
9+
this->next = this->prev = NULL;
10+
Subscribe(NULL, NULL);
11+
}
12+
13+
Sensor::~Sensor()
14+
{
15+
}
16+
17+
int Sensor::GetType(void)
18+
{
19+
return this->type;
20+
}
21+
22+
int Sensor::Subscribe(SensorEventHandler_t *handler, void* user_data)
23+
{
24+
this->evtHandler = handler;
25+
this->userData = user_data;
26+
27+
return 0;
28+
}
29+
30+
int Sensor::Publish(sensors_event_t* event)
31+
{
32+
if (this->evtHandler != NULL)
33+
{
34+
/* invoke subscribed handler */
35+
(*evtHandler)(this, event, this->userData);
36+
}
37+
38+
return 0;
39+
}
40+
41+
/**
42+
* Sensor Manager
43+
*/
44+
/* sensor manager instance */
45+
static SensorManager _sensor_manager;
46+
47+
SensorManager::SensorManager()
48+
{
49+
sensorList = NULL;
50+
}
51+
52+
SensorManager::~SensorManager()
53+
{
54+
}
55+
56+
int SensorManager::RegisterSensor(Sensor* sensor)
57+
{
58+
SensorManager* self = &_sensor_manager;
59+
60+
RT_ASSERT(sensor != RT_NULL);
61+
62+
/* add sensor into the list */
63+
if (self->sensorList = NULL)
64+
{
65+
sensor->prev = sensor->next = sensor;
66+
}
67+
else
68+
{
69+
sensor->prev = self->sensorList;
70+
sensor->next = self->sensorList->next;
71+
72+
self->sensorList->next->prev = sensor;
73+
self->sensorList->next = sensor;
74+
}
75+
76+
/* point the sensorList to this sensor */
77+
self->sensorList = sensor;
78+
79+
return 0;
80+
}
81+
82+
int SensorManager::DeregisterSensor(Sensor* sensor)
83+
{
84+
SensorManager* self = &_sensor_manager;
85+
86+
/* disconnect sensor list */
87+
sensor->next->prev = sensor->prev;
88+
sensor->prev->next = sensor->next;
89+
90+
/* check the sensorList */
91+
if (sensor == self->sensorList)
92+
{
93+
if (sensor->next == sensor) self->sensorList = NULL; /* empty list */
94+
else self->sensorList = sensor->next;
95+
}
96+
97+
/* re-initialize sensor node */
98+
sensor->next = sensor->prev = sensor;
99+
100+
return 0;
101+
}
102+
103+
Sensor *SensorManager::GetDefaultSensor(int type)
104+
{
105+
SensorManager* self = &_sensor_manager;
106+
Sensor *sensor = self->sensorList;
107+
108+
if (sensor == NULL) return NULL;
109+
110+
do
111+
{
112+
/* find the same type */
113+
if (sensor->GetType() == type) return sensor;
114+
115+
sensor = sensor->next;
116+
}
117+
while (sensor != self->sensorList);
118+
119+
return NULL;
120+
}
121+
122+
int SensorManager::Subscribe(int type, SensorEventHandler_t *handler, void* user_data)
123+
{
124+
Sensor *sensor;
125+
126+
sensor = SensorManager::GetDefaultSensor(type);
127+
if (sensor != NULL)
128+
{
129+
sensor->Subscribe(handler, user_data);
130+
return 0;
131+
}
132+
133+
return -1;
134+
}
135+

0 commit comments

Comments
 (0)