Skip to content
This repository was archived by the owner on Aug 22, 2023. It is now read-only.

Commit 372c7b7

Browse files
committed
add check ip address before casting
1 parent fa7fd50 commit 372c7b7

File tree

2 files changed

+20
-28
lines changed

2 files changed

+20
-28
lines changed

src/esp8266-google-home-notifier.cpp

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,7 @@
22

33
char data[1024];
44

5-
boolean GoogleHomeNotifier::device(const char * name)
6-
{
7-
return this->device(name, "en");
8-
}
9-
10-
boolean GoogleHomeNotifier::device(const char * name, const char * locale)
11-
{
12-
return GoogleHomeNotifier::device(name, locale, 10000);
13-
}
14-
15-
boolean GoogleHomeNotifier::device(const char * name, const char * locale, int to)
5+
boolean GoogleHomeNotifier::device(const char *name, const char *locale = "en", int to = 10000)
166
{
177
int timeout = millis() + to;
188
int n;
@@ -57,25 +47,29 @@ boolean GoogleHomeNotifier::device(const char * name, const char * locale, int t
5747
return true;
5848
}
5949

60-
boolean GoogleHomeNotifier::ip(IPAddress ip, const char *locale)
50+
boolean GoogleHomeNotifier::ip(IPAddress ip, const char *locale = "en", uint16_t port = 8009)
6151
{
6252
this->m_ipaddress = ip;
63-
this->m_port = 8009;
53+
this->m_port = port;
6454
sprintf(this->m_locale, "%s", locale);
6555
return true;
6656
}
6757

68-
boolean GoogleHomeNotifier::notify(const char * phrase) {
58+
boolean GoogleHomeNotifier::notify(const char *phrase) {
6959
return this->cast(phrase, NULL);
7060
}
7161

72-
boolean GoogleHomeNotifier::play(const char * mp3Url) {
62+
boolean GoogleHomeNotifier::play(const char *mp3Url) {
7363
return this->cast(NULL, mp3Url);
7464
}
7565

76-
boolean GoogleHomeNotifier::cast(const char * phrase, const char * mp3Url)
66+
boolean GoogleHomeNotifier::cast(const char *phrase, const char *mp3Url)
7767
{
7868
char error[128];
69+
if((this->m_ipaddress[0] == 0 && this->m_ipaddress[1] == 0 && this->m_ipaddress[2] == 0 && this->m_ipaddress[3] == 0) || this->m_port == 0) {
70+
this->setLastError("Google Home's IP address/port is not set. Call 'device' or 'ip' method before calling 'cast' method.");
71+
return false;
72+
}
7973
String speechUrl;
8074
if (phrase != NULL) {
8175
speechUrl = tts.getSpeechUrl(phrase, m_locale);
@@ -131,7 +125,7 @@ const uint16_t GoogleHomeNotifier::getPort()
131125
return m_port;
132126
}
133127

134-
boolean GoogleHomeNotifier::sendMessage(const char* sourceId, const char* destinationId, const char* ns, const char* data)
128+
boolean GoogleHomeNotifier::sendMessage(const char *sourceId, const char *destinationId, const char *ns, const char *data)
135129
{
136130
extensions_api_cast_channel_CastMessage message = extensions_api_cast_channel_CastMessage_init_default;
137131

@@ -267,7 +261,7 @@ boolean GoogleHomeNotifier::connect()
267261
return true;
268262
}
269263

270-
boolean GoogleHomeNotifier::_play(const char * mp3url)
264+
boolean GoogleHomeNotifier::_play(const char *mp3url)
271265
{
272266
// send 'CONNECT' again
273267
sprintf(data, CASTV2_DATA_CONNECT);

src/esp8266-google-home-notifier.h

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,17 @@ typedef class GoogleHomeNotifier {
5252
static bool encode_string(pb_ostream_t *stream, const pb_field_t *field, void * const *arg);
5353
static bool decode_string(pb_istream_t *stream, const pb_field_t *field, void **arg);
5454
boolean connect();
55-
boolean _play(const char* mp3url);
55+
boolean _play(const char *mp3url);
5656
void disconnect();
57-
void setLastError(const char* lastError);
58-
boolean sendMessage(const char* sourceId, const char* destinationId, const char* ns, const char* data);
59-
boolean cast(const char * phrase, const char * mp3Url);
57+
void setLastError(const char *lastError);
58+
boolean sendMessage(const char *sourceId, const char *destinationId, const char *ns, const char *data);
59+
boolean cast(const char *phrase, const char *mp3Url);
6060

6161
public:
62-
boolean device(const char * name); // locale = 'en', timeout = 10000
63-
boolean device(const char * name, const char * locale); // timeout = 10000
64-
boolean device(const char * name, const char * locale, int timeout);
65-
boolean ip(IPAddress ip, const char *locale);
66-
boolean notify(const char * phrase);
67-
boolean play(const char * mp3Url);
62+
boolean device(const char *name, const char *locale /* = 'en' */, int timeout /* = 10000 */);
63+
boolean ip(IPAddress ip, const char *locale /* = 'en' */, uint16_t port /* = 8009 */ );
64+
boolean notify(const char *phrase);
65+
boolean play(const char *mp3Url);
6866
const IPAddress getIPAddress();
6967
const uint16_t getPort();
7068
const char * getLastError();

0 commit comments

Comments
 (0)