Skip to content

Commit f271ac7

Browse files
committed
Change to template structure
Audio class (and other feature sets) now uses templating instead of virtual inheritance
1 parent f3e7109 commit f271ac7

File tree

4 files changed

+122
-129
lines changed

4 files changed

+122
-129
lines changed

src/sfe_lara_r6.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
#include "sfe_ublox_cellular_voice.h"
66

77
// Base LARA-R6 class
8-
class LARA_R6: virtual public UBX_CELL
8+
class LARA_R6: public UBX_CELL
99
{
1010

1111
};
1212

13-
class LARA_R6001: public LARA_R6, public UBX_CELL_VOICE
13+
class LARA_R6001: public LARA_R6, public UBX_CELL_VOICE<LARA_R6001>
1414
{
1515

1616
};
@@ -20,7 +20,7 @@ class LARA_R6001D: public LARA_R6
2020

2121
};
2222

23-
class LARA_R6401: public LARA_R6, public UBX_CELL_VOICE
23+
class LARA_R6401: public LARA_R6, public UBX_CELL_VOICE<LARA_R6401>
2424
{
2525

2626
};
@@ -30,7 +30,7 @@ class LARA_R6401D: public LARA_R6
3030

3131
};
3232

33-
class LARA_R6801_00B: public LARA_R6, public UBX_CELL_VOICE
33+
class LARA_R6801_00B: public LARA_R6, public UBX_CELL_VOICE<LARA_R6801_00B>
3434
{
3535

3636
};

src/sfe_ublox_cellular.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,12 @@ class UBX_CELL : public Print
10241024
UBX_CELL_error_t sendCustomCommandWithResponse(const char *command, const char *expectedResponse,
10251025
char *responseDest, unsigned long commandTimeout = UBX_CELL_STANDARD_RESPONSE_TIMEOUT, bool at = true);
10261026

1027+
// Send command with an expected (potentially partial) response, store entire response
1028+
UBX_CELL_error_t sendCommandWithResponse(const char *command, const char *expectedResponse,
1029+
char *responseDest, unsigned long commandTimeout, int destSize = minimumResponseAllocation, bool at = true);
1030+
1031+
char *ubx_cell_calloc_char(size_t num);
1032+
10271033
protected:
10281034
HardwareSerial *_hardSerial;
10291035
#ifdef UBX_CELL_SOFTWARE_SERIAL_ENABLED
@@ -1091,10 +1097,6 @@ class UBX_CELL : public Print
10911097
// Wait for an expected response (don't send a command)
10921098
UBX_CELL_error_t waitForResponse(const char *expectedResponse, const char *expectedError, uint16_t timeout);
10931099

1094-
// Send command with an expected (potentially partial) response, store entire response
1095-
UBX_CELL_error_t sendCommandWithResponse(const char *command, const char *expectedResponse,
1096-
char *responseDest, unsigned long commandTimeout, int destSize = minimumResponseAllocation, bool at = true);
1097-
10981100
// Send a command -- prepend AT if at is true
10991101
void sendCommand(const char *command, bool at);
11001102

@@ -1118,8 +1120,6 @@ class UBX_CELL : public Print
11181120

11191121
UBX_CELL_error_t autobaud(unsigned long desiredBaud);
11201122

1121-
char *ubx_cell_calloc_char(size_t num);
1122-
11231123
bool processURCEvent(const char *event);
11241124
void pruneBacklog(void);
11251125

src/sfe_ublox_cellular_voice.cpp

Lines changed: 0 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1 @@
11
#include "sfe_ublox_cellular_voice.h"
2-
3-
UBX_CELL_error_t UBX_CELL_VOICE::dial(String number)
4-
{
5-
char *command;
6-
char *numberCStr;
7-
UBX_CELL_error_t err;
8-
9-
numberCStr = ubx_cell_calloc_char(number.length() + 1);
10-
if (numberCStr == nullptr)
11-
return UBX_CELL_ERROR_OUT_OF_MEMORY;
12-
number.toCharArray(numberCStr, number.length() + 1);
13-
14-
command = ubx_cell_calloc_char(strlen(UBX_CELL_COMMAND_DIAL) + strlen(numberCStr) + 3);
15-
if (command != nullptr)
16-
{
17-
// Heads up! The dial command is one of the only commands that requires a
18-
// semicolon at the end of it!
19-
sprintf(command, "%s=%s;", UBX_CELL_COMMAND_DIAL, numberCStr);
20-
21-
err = sendCommandWithResponse(command, UBX_CELL_RESPONSE_OK,
22-
nullptr, UBX_CELL_10_SEC_TIMEOUT);
23-
24-
free(command);
25-
}
26-
else
27-
{
28-
err = UBX_CELL_ERROR_OUT_OF_MEMORY;
29-
}
30-
31-
free(numberCStr);
32-
33-
return err;
34-
}
35-
36-
UBX_CELL_error_t UBX_CELL_VOICE::answer(void)
37-
{
38-
return sendCommandWithResponse(UBX_CELL_COMMAND_ANSWER, UBX_CELL_RESPONSE_OK_OR_ERROR,
39-
nullptr, UBX_CELL_STANDARD_RESPONSE_TIMEOUT);
40-
}
41-
42-
UBX_CELL_error_t UBX_CELL_VOICE::hangUp(void)
43-
{
44-
return sendCommandWithResponse(UBX_CELL_COMMAND_HANG_UP, UBX_CELL_RESPONSE_OK_OR_ERROR,
45-
nullptr, UBX_CELL_STANDARD_RESPONSE_TIMEOUT);
46-
}
47-
48-
UBX_CELL_error_t UBX_CELL_VOICE::playAudioResource(uint8_t audio_resource, uint8_t tone_id, uint8_t nof_repeat)
49-
{
50-
UBX_CELL_error_t err;
51-
char *command;
52-
53-
command = ubx_cell_calloc_char(strlen(UBX_CELL_COMMAND_PLAY_AUDIO) + 13);
54-
if (command == nullptr)
55-
return UBX_CELL_ERROR_OUT_OF_MEMORY;
56-
sprintf(command, "%s=%d,%d,%d", UBX_CELL_COMMAND_PLAY_AUDIO, audio_resource, tone_id, nof_repeat);
57-
58-
err = sendCommandWithResponse(command, UBX_CELL_RESPONSE_OK_OR_ERROR,
59-
nullptr, UBX_CELL_STANDARD_RESPONSE_TIMEOUT);
60-
free(command);
61-
return err;
62-
}
63-
64-
UBX_CELL_error_t UBX_CELL_VOICE::stopAudioResource(uint8_t audio_resource)
65-
{
66-
UBX_CELL_error_t err;
67-
char *command;
68-
69-
command = ubx_cell_calloc_char(strlen(UBX_CELL_COMMAND_STOP_AUDIO) + 5);
70-
if (command == nullptr)
71-
return UBX_CELL_ERROR_OUT_OF_MEMORY;
72-
sprintf(command, "%s=%d", UBX_CELL_COMMAND_STOP_AUDIO, audio_resource);
73-
74-
err = sendCommandWithResponse(command, UBX_CELL_RESPONSE_OK_OR_ERROR,
75-
nullptr, UBX_CELL_STANDARD_RESPONSE_TIMEOUT);
76-
free(command);
77-
return err;
78-
}
79-
80-
UBX_CELL_error_t UBX_CELL_VOICE::generateToneFreq(uint16_t frequency, uint16_t duration, uint8_t volume)
81-
{
82-
UBX_CELL_error_t err;
83-
char *command;
84-
char response[] = "\r\nOK\r\n\r\n+UUTGN: 0\r\n";
85-
86-
command = ubx_cell_calloc_char(strlen(UBX_CELL_COMMAND_GENERATE_TONE) + 15);
87-
if (command == nullptr)
88-
return UBX_CELL_ERROR_OUT_OF_MEMORY;
89-
sprintf(command, "%s=%d,%d,%d", UBX_CELL_COMMAND_GENERATE_TONE, frequency, duration, volume);
90-
91-
err = sendCommandWithResponse(command, response,
92-
nullptr, UBX_CELL_STANDARD_RESPONSE_TIMEOUT);
93-
free(command);
94-
return err;
95-
}
96-
97-
UBX_CELL_error_t UBX_CELL_VOICE::generateToneDTMF(char dtmf_character, uint16_t duration, uint8_t volume)
98-
{
99-
UBX_CELL_error_t err;
100-
char *command;
101-
char response[] = "\r\nOK\r\n\r\n+UUTGN: 0\r\n";
102-
103-
command = ubx_cell_calloc_char(strlen(UBX_CELL_COMMAND_GENERATE_TONE) + 14);
104-
if (command == nullptr)
105-
return UBX_CELL_ERROR_OUT_OF_MEMORY;
106-
sprintf(command, "%s=\"%c\",%d,%d", UBX_CELL_COMMAND_GENERATE_TONE, dtmf_character, duration, volume);
107-
108-
err = sendCommandWithResponse(command, response,
109-
nullptr, UBX_CELL_STANDARD_RESPONSE_TIMEOUT);
110-
free(command);
111-
return err;
112-
}

src/sfe_ublox_cellular_voice.h

Lines changed: 112 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,120 @@ const char UBX_CELL_COMMAND_STOP_AUDIO[] = "+USAR"; // Stop audio resource
1111
const char UBX_CELL_COMMAND_GENERATE_TONE[] = "+UTGN"; // Tone generator
1212

1313
// Base class for any modules supporting voice calls
14-
class UBX_CELL_VOICE: virtual public UBX_CELL
14+
template <typename T>
15+
class UBX_CELL_VOICE
1516
{
1617
public:
17-
UBX_CELL_error_t dial(String number);
18-
UBX_CELL_error_t answer(void);
19-
UBX_CELL_error_t hangUp(void);
20-
UBX_CELL_error_t playAudioResource(uint8_t audio_resource, uint8_t tone_id, uint8_t nof_repeat);
21-
UBX_CELL_error_t stopAudioResource(uint8_t audio_resource);
22-
UBX_CELL_error_t generateToneFreq(uint16_t frequency, uint16_t duration, uint8_t volume);
23-
UBX_CELL_error_t generateToneDTMF(char dtmf_character, uint16_t duration, uint8_t volume);
18+
UBX_CELL_error_t dial(String number)
19+
{
20+
char *command;
21+
char *numberCStr;
22+
UBX_CELL_error_t err;
23+
24+
numberCStr = static_cast<T*>(this)->ubx_cell_calloc_char(number.length() + 1);
25+
if (numberCStr == nullptr)
26+
return UBX_CELL_ERROR_OUT_OF_MEMORY;
27+
number.toCharArray(numberCStr, number.length() + 1);
28+
29+
command = static_cast<T*>(this)->ubx_cell_calloc_char(strlen(UBX_CELL_COMMAND_DIAL) + strlen(numberCStr) + 3);
30+
if (command != nullptr)
31+
{
32+
// Heads up! The dial command is one of the only commands that requires a
33+
// semicolon at the end of it!
34+
sprintf(command, "%s=%s;", UBX_CELL_COMMAND_DIAL, numberCStr);
35+
36+
err = static_cast<T*>(this)->sendCommandWithResponse(command, UBX_CELL_RESPONSE_OK,
37+
nullptr, UBX_CELL_10_SEC_TIMEOUT);
38+
39+
free(command);
40+
}
41+
else
42+
{
43+
err = UBX_CELL_ERROR_OUT_OF_MEMORY;
44+
}
45+
46+
free(numberCStr);
47+
48+
return err;
49+
}
50+
51+
UBX_CELL_error_t answer(void)
52+
{
53+
return static_cast<T*>(this)->sendCommandWithResponse(UBX_CELL_COMMAND_ANSWER, UBX_CELL_RESPONSE_OK_OR_ERROR,
54+
nullptr, UBX_CELL_STANDARD_RESPONSE_TIMEOUT);
55+
}
56+
57+
UBX_CELL_error_t hangUp(void)
58+
{
59+
return static_cast<T*>(this)->sendCommandWithResponse(UBX_CELL_COMMAND_HANG_UP, UBX_CELL_RESPONSE_OK_OR_ERROR,
60+
nullptr, UBX_CELL_STANDARD_RESPONSE_TIMEOUT);
61+
}
62+
63+
UBX_CELL_error_t playAudioResource(uint8_t audio_resource, uint8_t tone_id, uint8_t nof_repeat)
64+
{
65+
UBX_CELL_error_t err;
66+
char *command;
67+
68+
command = static_cast<T*>(this)->ubx_cell_calloc_char(strlen(UBX_CELL_COMMAND_PLAY_AUDIO) + 13);
69+
if (command == nullptr)
70+
return UBX_CELL_ERROR_OUT_OF_MEMORY;
71+
sprintf(command, "%s=%d,%d,%d", UBX_CELL_COMMAND_PLAY_AUDIO, audio_resource, tone_id, nof_repeat);
72+
73+
err = static_cast<T*>(this)->sendCommandWithResponse(command, UBX_CELL_RESPONSE_OK_OR_ERROR,
74+
nullptr, UBX_CELL_STANDARD_RESPONSE_TIMEOUT);
75+
free(command);
76+
return err;
77+
}
78+
79+
UBX_CELL_error_t stopAudioResource(uint8_t audio_resource)
80+
{
81+
UBX_CELL_error_t err;
82+
char *command;
83+
84+
command = static_cast<T*>(this)->ubx_cell_calloc_char(strlen(UBX_CELL_COMMAND_STOP_AUDIO) + 5);
85+
if (command == nullptr)
86+
return UBX_CELL_ERROR_OUT_OF_MEMORY;
87+
sprintf(command, "%s=%d", UBX_CELL_COMMAND_STOP_AUDIO, audio_resource);
88+
89+
err = static_cast<T*>(this)->sendCommandWithResponse(command, UBX_CELL_RESPONSE_OK_OR_ERROR,
90+
nullptr, UBX_CELL_STANDARD_RESPONSE_TIMEOUT);
91+
free(command);
92+
return err;
93+
}
94+
95+
UBX_CELL_error_t generateToneFreq(uint16_t frequency, uint16_t duration, uint8_t volume)
96+
{
97+
UBX_CELL_error_t err;
98+
char *command;
99+
char response[] = "\r\nOK\r\n\r\n+UUTGN: 0\r\n";
100+
101+
command = static_cast<T*>(this)->ubx_cell_calloc_char(strlen(UBX_CELL_COMMAND_GENERATE_TONE) + 15);
102+
if (command == nullptr)
103+
return UBX_CELL_ERROR_OUT_OF_MEMORY;
104+
sprintf(command, "%s=%d,%d,%d", UBX_CELL_COMMAND_GENERATE_TONE, frequency, duration, volume);
105+
106+
err = static_cast<T*>(this)->sendCommandWithResponse(command, response,
107+
nullptr, UBX_CELL_STANDARD_RESPONSE_TIMEOUT);
108+
free(command);
109+
return err;
110+
}
111+
112+
UBX_CELL_error_t generateToneDTMF(char dtmf_character, uint16_t duration, uint8_t volume)
113+
{
114+
UBX_CELL_error_t err;
115+
char *command;
116+
char response[] = "\r\nOK\r\n\r\n+UUTGN: 0\r\n";
117+
118+
command = static_cast<T*>(this)->ubx_cell_calloc_char(strlen(UBX_CELL_COMMAND_GENERATE_TONE) + 14);
119+
if (command == nullptr)
120+
return UBX_CELL_ERROR_OUT_OF_MEMORY;
121+
sprintf(command, "%s=\"%c\",%d,%d", UBX_CELL_COMMAND_GENERATE_TONE, dtmf_character, duration, volume);
122+
123+
err = static_cast<T*>(this)->sendCommandWithResponse(command, response,
124+
nullptr, UBX_CELL_STANDARD_RESPONSE_TIMEOUT);
125+
free(command);
126+
return err;
127+
}
24128
};
25129

26130
#endif

0 commit comments

Comments
 (0)