“HTTP Status 404 - No result defined for action ****** and result result”错误解决方法

本文详细解析了Struts2框架中Struts.xml配置文件的问题,特别是对于namespace和extends属性的正确使用进行了说明,并给出了正确的配置示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个问题困扰了一个下午,晚上在网上找了一些Struts入门教程看了一下,才发现问题关键,是因为我Struts.xml配置不对的缘故。我原来的Struts.xml代码如下:

xml version="1.0" encoding="UTF-8" ?>
DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
    <package name="Struts2" extends="struts-default">
        <action name="query"  class="chapter7.action.QueryAction">
            <result name="result">/result.jspresult>
            <result name="error">/error.jspresult>
        action>package> struts> 

需要将上面的代码改成如下代码:

xml version="1.0" encoding="UTF-8" ?>
DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
    <package name="Struts2" namespace="/" extends="struts-default">
        <action name="query"  class="chapter7.action.QueryAction">
            <result name="result">/result.jspresult>
            <result name="error">/error.jspresult>
        action>package> struts> 

即 namespace="/" extends="struts-default">。

action界面是通过 http://localhost:8080/StrutsTest1/query.action 来访问的。如果这里namespace改为namespace="/hello" ,则action界面的访问地址应该改为 http://localhost:8080/StrutsTest1/hello/query.action




本文转自xwdreamer博客园博客,原文链接:http://www.cnblogs.com/xwdreamer/archive/2010/09/14/2297091.html,如需转载请自行联系原作者


/* stdlib.h: ANSI draft (X3J11 May 88) library header, section 4.10 */ /* Copyright (C) Codemist Ltd., 1988-1993. */ /* Copyright 1991-1998,2014 ARM Limited. All rights reserved. */ /* * RCS $Revision$ * Checkin $Date$ * Revising $Author: agrant $ */ /* * stdlib.h declares four types, several general purpose functions, * and defines several macros. */ #ifndef __stdlib_h #define __stdlib_h #define __ARMCLIB_VERSION 5060034 #if defined(__clang__) || (defined(__ARMCC_VERSION) && !defined(__STRICT_ANSI__)) /* armclang and non-strict armcc allow 'long long' in system headers */ #define __LONGLONG long long #else /* strict armcc has '__int64' */ #define __LONGLONG __int64 #endif #define _ARMABI __declspec(__nothrow) #define _ARMABI_PURE __declspec(__nothrow) __attribute__((const)) #define _ARMABI_NORETURN __declspec(__nothrow) __declspec(__noreturn) #define _ARMABI_THROW #ifndef __STDLIB_DECLS #define __STDLIB_DECLS /* * Some of these declarations are new in C99. To access them in C++ * you can use -D__USE_C99_STDLIB (or -D__USE_C99ALL). */ #ifndef __USE_C99_STDLIB #if defined(__USE_C99_ALL) || (defined(__STDC_VERSION__) && 199901L <= __STDC_VERSION__) || (defined(__cplusplus) && 201103L <= __cplusplus) #define __USE_C99_STDLIB 1 #endif #endif #undef __CLIBNS #ifdef __cplusplus namespace std { #define __CLIBNS ::std:: extern "C" { #else #define __CLIBNS #endif /* __cplusplus */ #if defined(__cplusplus) || !defined(__STRICT_ANSI__) /* unconditional in C++ and non-strict C for consistency of debug info */ #if __sizeof_ptr == 8 typedef unsigned long size_t; /* see <stddef.h> */ #else typedef unsigned int size_t; /* see <stddef.h> */ #endif #elif !defined(__size_t) #define __size_t 1 #if __sizeof_ptr == 8 typedef unsigned long size_t; /* see <stddef.h> */ #else typedef unsigned int size_t; /* see <stddef.h> */ #endif #endif #undef NULL #define NULL 0 /* see <stddef.h> */ #ifndef __cplusplus /* wchar_t is a builtin type for C++ */ #if !defined(__STRICT_ANSI__) /* unconditional in non-strict C for consistency of debug info */ #if defined(__WCHAR32) || (defined(__ARM_SIZEOF_WCHAR_T) && __ARM_SIZEOF_WCHAR_T == 4) typedef unsigned int wchar_t; /* see <stddef.h> */ #else typedef unsigned short wchar_t; /* see <stddef.h> */ #endif #elif !defined(__wchar_t) #define __wchar_t 1 #if defined(__WCHAR32) || (defined(__ARM_SIZEOF_WCHAR_T) && __ARM_SIZEOF_WCHAR_T == 4) typedef unsigned int wchar_t; /* see <stddef.h> */ #else typedef unsigned short wchar_t; /* see <stddef.h> */ #endif #endif #endif typedef struct div_t { int quot, rem; } div_t; /* type of the value returned by the div function. */ typedef struct ldiv_t { long int quot, rem; } ldiv_t; /* type of the value returned by the ldiv function. */ #if !defined(__STRICT_ANSI__) || __USE_C99_STDLIB typedef struct lldiv_t { __LONGLONG quot, rem; } lldiv_t; /* type of the value returned by the lldiv function. */ #endif #ifdef __EXIT_FAILURE # define EXIT_FAILURE __EXIT_FAILURE /* * an integral expression which may be used as an argument to the exit * function to return unsuccessful termination status to the host * environment. */ #else # define EXIT_FAILURE 1 /* unixoid */ #endif #define EXIT_SUCCESS 0 /* * an integral expression which may be used as an argument to the exit * function to return successful termination status to the host * environment. */ /* * Defining __USE_ANSI_EXAMPLE_RAND at compile time switches to * the example implementation of rand() and srand() provided in * the ANSI C standard. This implementation is very poor, but is * provided for completeness. */ #ifdef __USE_ANSI_EXAMPLE_RAND #define srand _ANSI_srand #define rand _ANSI_rand #define RAND_MAX 0x7fff #else #define RAND_MAX 0x7fffffff #endif /* * RAND_MAX: an integral constant expression, the value of which * is the maximum value returned by the rand function. */ extern _ARMABI int __aeabi_MB_CUR_MAX(void); #define MB_CUR_MAX ( __aeabi_MB_CUR_MAX() ) /* * a positive integer expression whose value is the maximum number of bytes * in a multibyte character for the extended character set specified by the * current locale (category LC_CTYPE), and whose value is never greater * than MB_LEN_MAX. */ /* * If the compiler supports signalling nans as per N965 then it * will define __SUPPORT_SNAN__, in which case a user may define * _WANT_SNAN in order to obtain a compliant version of the strtod * family of functions. */ #if defined(__SUPPORT_SNAN__) && defined(_WANT_SNAN) #pragma import(__use_snan) #endif extern _ARMABI double atof(const char * /*nptr*/) __attribute__((__nonnull__(1))); /* * converts the initial part of the string pointed to by nptr to double * representation. * Returns: the converted value. */ extern _ARMABI int atoi(const char * /*nptr*/) __attribute__((__nonnull__(1))); /* * converts the initial part of the string pointed to by nptr to int * representation. * Returns: the converted value. */ extern _ARMABI long int atol(const char * /*nptr*/) __attribute__((__nonnull__(1))); /* * converts the initial part of the string pointed to by nptr to long int * representation. * Returns: the converted value. */ #if !defined(__STRICT_ANSI__) || __USE_C99_STDLIB extern _ARMABI __LONGLONG atoll(const char * /*nptr*/) __attribute__((__nonnull__(1))); /* * converts the initial part of the string pointed to by nptr to * long long int representation. * Returns: the converted value. */ #endif extern _ARMABI double strtod(const char * __restrict /*nptr*/, char ** __restrict /*endptr*/) __attribute__((__nonnull__(1))); /* * converts the initial part of the string pointed to by nptr to double * representation. First it decomposes the input string into three parts: * an initial, possibly empty, sequence of white-space characters (as * specified by the isspace function), a subject sequence resembling a * floating point constant; and a final string of one or more unrecognised * characters, including the terminating null character of the input string. * Then it attempts to convert the subject sequence to a floating point * number, and returns the result. A pointer to the final string is stored * in the object pointed to by endptr, provided that endptr is not a null * pointer. * Returns: the converted value if any. If no conversion could be performed, * zero is returned. If the correct value is outside the range of * representable values, plus or minus HUGE_VAL is returned * (according to the sign of the value), and the value of the macro * ERANGE is stored in errno. If the correct value would cause * underflow, zero is returned and the value of the macro ERANGE is * stored in errno. */ #if !defined(__STRICT_ANSI__) || __USE_C99_STDLIB extern _ARMABI float strtof(const char * __restrict /*nptr*/, char ** __restrict /*endptr*/) __attribute__((__nonnull__(1))); extern _ARMABI long double strtold(const char * __restrict /*nptr*/, char ** __restrict /*endptr*/) __attribute__((__nonnull__(1))); /* * same as strtod, but return float and long double respectively. */ #endif extern _ARMABI long int strtol(const char * __restrict /*nptr*/, char ** __restrict /*endptr*/, int /*base*/) __attribute__((__nonnull__(1))); /* * converts the initial part of the string pointed to by nptr to long int * representation. First it decomposes the input string into three parts: * an initial, possibly empty, sequence of white-space characters (as * specified by the isspace function), a subject sequence resembling an * integer represented in some radix determined by the value of base, and a * final string of one or more unrecognised characters, including the * terminating null character of the input string. Then it attempts to * convert the subject sequence to an integer, and returns the result. * If the value of base is 0, the expected form of the subject sequence is * that of an integer constant (described in ANSI Draft, section 3.1.3.2), * optionally preceded by a '+' or '-' sign, but not including an integer * suffix. If the value of base is between 2 and 36, the expected form of * the subject sequence is a sequence of letters and digits representing an * integer with the radix specified by base, optionally preceded by a plus * or minus sign, but not including an integer suffix. The letters from a * (or A) through z (or Z) are ascribed the values 10 to 35; only letters * whose ascribed values are less than that of the base are permitted. If * the value of base is 16, the characters 0x or 0X may optionally precede * the sequence of letters and digits following the sign if present. * A pointer to the final string is stored in the object * pointed to by endptr, provided that endptr is not a null pointer. * Returns: the converted value if any. If no conversion could be performed, * zero is returned and nptr is stored in *endptr. * If the correct value is outside the range of * representable values, LONG_MAX or LONG_MIN is returned * (according to the sign of the value), and the value of the * macro ERANGE is stored in errno. */ extern _ARMABI unsigned long int strtoul(const char * __restrict /*nptr*/, char ** __restrict /*endptr*/, int /*base*/) __attribute__((__nonnull__(1))); /* * converts the initial part of the string pointed to by nptr to unsigned * long int representation. First it decomposes the input string into three * parts: an initial, possibly empty, sequence of white-space characters (as * determined by the isspace function), a subject sequence resembling an * unsigned integer represented in some radix determined by the value of * base, and a final string of one or more unrecognised characters, * including the terminating null character of the input string. Then it * attempts to convert the subject sequence to an unsigned integer, and * returns the result. If the value of base is zero, the expected form of * the subject sequence is that of an integer constant (described in ANSI * Draft, section 3.1.3.2), optionally preceded by a '+' or '-' sign, but * not including an integer suffix. If the value of base is between 2 and * 36, the expected form of the subject sequence is a sequence of letters * and digits representing an integer with the radix specified by base, * optionally preceded by a '+' or '-' sign, but not including an integer * suffix. The letters from a (or A) through z (or Z) stand for the values * 10 to 35; only letters whose ascribed values are less than that of the * base are permitted. If the value of base is 16, the characters 0x or 0X * may optionally precede the sequence of letters and digits following the * sign, if present. A pointer to the final string is stored in the object * pointed to by endptr, provided that endptr is not a null pointer. * Returns: the converted value if any. If no conversion could be performed, * zero is returned and nptr is stored in *endptr. * If the correct value is outside the range of * representable values, ULONG_MAX is returned, and the value of * the macro ERANGE is stored in errno. */ /* C90 reserves all names beginning with 'str' */ extern _ARMABI __LONGLONG strtoll(const char * __restrict /*nptr*/, char ** __restrict /*endptr*/, int /*base*/) __attribute__((__nonnull__(1))); /* * as strtol but returns a long long int value. If the correct value is * outside the range of representable values, LLONG_MAX or LLONG_MIN is * returned (according to the sign of the value), and the value of the * macro ERANGE is stored in errno. */ extern _ARMABI unsigned __LONGLONG strtoull(const char * __restrict /*nptr*/, char ** __restrict /*endptr*/, int /*base*/) __attribute__((__nonnull__(1))); /* * as strtoul but returns an unsigned long long int value. If the correct * value is outside the range of representable values, ULLONG_MAX is returned, * and the value of the macro ERANGE is stored in errno. */ extern _ARMABI int rand(void); /* * Computes a sequence of pseudo-random integers in the range 0 to RAND_MAX. * Uses an additive generator (Mitchell & Moore) of the form: * Xn = (X[n-24] + X[n-55]) MOD 2^31 * This is described in section 3.2.2 of Knuth, vol 2. It's period is * in excess of 2^55 and its randomness properties, though unproven, are * conjectured to be good. Empirical testing since 1958 has shown no flaws. * Returns: a pseudo-random integer. */ extern _ARMABI void srand(unsigned int /*seed*/); /* * uses its argument as a seed for a new sequence of pseudo-random numbers * to be returned by subsequent calls to rand. If srand is then called with * the same seed value, the sequence of pseudo-random numbers is repeated. * If rand is called before any calls to srand have been made, the same * sequence is generated as when srand is first called with a seed value * of 1. */ struct _rand_state { int __x[57]; }; extern _ARMABI int _rand_r(struct _rand_state *); extern _ARMABI void _srand_r(struct _rand_state *, unsigned int); struct _ANSI_rand_state { int __x[1]; }; extern _ARMABI int _ANSI_rand_r(struct _ANSI_rand_state *); extern _ARMABI void _ANSI_srand_r(struct _ANSI_rand_state *, unsigned int); /* * Re-entrant variants of both flavours of rand, which operate on * an explicitly supplied state buffer. */ extern _ARMABI void *calloc(size_t /*nmemb*/, size_t /*size*/); /* * allocates space for an array of nmemb objects, each of whose size is * 'size'. The space is initialised to all bits zero. * Returns: either a null pointer or a pointer to the allocated space. */ extern _ARMABI void free(void * /*ptr*/); /* * causes the space pointed to by ptr to be deallocated (i.e., made * available for further allocation). If ptr is a null pointer, no action * occurs. Otherwise, if ptr does not match a pointer earlier returned by * calloc, malloc or realloc or if the space has been deallocated by a call * to free or realloc, the behaviour is undefined. */ extern _ARMABI void *malloc(size_t /*size*/); /* * allocates space for an object whose size is specified by 'size' and whose * value is indeterminate. * Returns: either a null pointer or a pointer to the allocated space. */ extern _ARMABI void *realloc(void * /*ptr*/, size_t /*size*/); /* * changes the size of the object pointed to by ptr to the size specified by * size. The contents of the object shall be unchanged up to the lesser of * the new and old sizes. If the new size is larger, the value of the newly * allocated portion of the object is indeterminate. If ptr is a null * pointer, the realloc function behaves like a call to malloc for the * specified size. Otherwise, if ptr does not match a pointer earlier * returned by calloc, malloc or realloc, or if the space has been * deallocated by a call to free or realloc, the behaviour is undefined. * If the space cannot be allocated, the object pointed to by ptr is * unchanged. If size is zero and ptr is not a null pointer, the object it * points to is freed. * Returns: either a null pointer or a pointer to the possibly moved * allocated space. */ #if !defined(__STRICT_ANSI__) extern _ARMABI int posix_memalign(void ** /*ret*/, size_t /*alignment*/, size_t /*size*/); /* * allocates space for an object of size 'size', aligned to a * multiple of 'alignment' (which must be a power of two and at * least 4). * * On success, a pointer to the allocated object is stored in * *ret, and zero is returned. On failure, the return value is * either ENOMEM (allocation failed because no suitable piece of * memory was available) or EINVAL (the 'alignment' parameter was * invalid). */ #endif typedef int (*__heapprt)(void *, char const *, ...); extern _ARMABI void __heapstats(int (* /*dprint*/)(void * /*param*/, char const * /*format*/, ...), void * /*param*/) __attribute__((__nonnull__(1))); /* * reports current heap statistics (eg. number of free blocks in * the free-list). Output is as implementation-defined free-form * text, provided via the dprint function. `param' gives an * extra data word to pass to dprint. You can call * __heapstats(fprintf,stdout) by casting fprintf to the above * function type; the typedef `__heapprt' is provided for this * purpose. * * `dprint' will not be called while the heap is being examined, * so it can allocate memory itself without trouble. */ extern _ARMABI int __heapvalid(int (* /*dprint*/)(void * /*param*/, char const * /*format*/, ...), void * /*param*/, int /*verbose*/) __attribute__((__nonnull__(1))); /* * performs a consistency check on the heap. Errors are reported * through dprint, like __heapstats. If `verbose' is nonzero, * full diagnostic information on the heap state is printed out. * * This routine probably won't work if the heap isn't a * contiguous chunk (for example, if __user_heap_extend has been * overridden). * * `dprint' may be called while the heap is being examined or * even in an invalid state, so it must perform no memory * allocation. In particular, if `dprint' calls (or is) a stdio * function, the stream it outputs to must already have either * been written to or been setvbuf'ed, or else the system will * allocate buffer space for it on the first call to dprint. */ extern _ARMABI_NORETURN void abort(void); /* * causes abnormal program termination to occur, unless the signal SIGABRT * is being caught and the signal handler does not return. Whether open * output streams are flushed or open streams are closed or temporary * files removed is implementation-defined. * An implementation-defined form of the status 'unsuccessful termination' * is returned to the host environment by means of a call to * raise(SIGABRT). */ extern _ARMABI int atexit(void (* /*func*/)(void)) __attribute__((__nonnull__(1))); /* * registers the function pointed to by func, to be called without its * arguments at normal program termination. It is possible to register at * least 32 functions. * Returns: zero if the registration succeeds, nonzero if it fails. */ #if defined(__EDG__) && !defined(__GNUC__) #define __LANGUAGE_LINKAGE_CHANGES_FUNCTION_TYPE #endif #if defined(__cplusplus) && defined(__LANGUAGE_LINKAGE_CHANGES_FUNCTION_TYPE) /* atexit that takes a ptr to a function with C++ linkage * but not in GNU mode */ typedef void (* __C_exitfuncptr)(); extern "C++" inline int atexit(void (* __func)()) { return atexit((__C_exitfuncptr)__func); } #endif extern _ARMABI_NORETURN void exit(int /*status*/); /* * causes normal program termination to occur. If more than one call to the * exit function is executed by a program, the behaviour is undefined. * First, all functions registered by the atexit function are called, in the * reverse order of their registration. * Next, all open output streams are flushed, all open streams are closed, * and all files created by the tmpfile function are removed. * Finally, control is returned to the host environment. If the value of * status is zero or EXIT_SUCCESS, an implementation-defined form of the * status 'successful termination' is returned. If the value of status is * EXIT_FAILURE, an implementation-defined form of the status * 'unsuccessful termination' is returned. Otherwise the status returned * is implementation-defined. */ extern _ARMABI_NORETURN void _Exit(int /*status*/); /* * causes normal program termination to occur. No functions registered * by the atexit function are called. * In this implementation, all open output streams are flushed, all * open streams are closed, and all files created by the tmpfile function * are removed. * Control is returned to the host environment. The status returned to * the host environment is determined in the same way as for 'exit'. */ extern _ARMABI char *getenv(const char * /*name*/) __attribute__((__nonnull__(1))); /* * searches the environment list, provided by the host environment, for a * string that matches the string pointed to by name. The set of environment * names and the method for altering the environment list are * implementation-defined. * Returns: a pointer to a string associated with the matched list member. * The array pointed to shall not be modified by the program, but * may be overwritten by a subsequent call to the getenv function. * If the specified name cannot be found, a null pointer is * returned. */ extern _ARMABI int system(const char * /*string*/); /* * passes the string pointed to by string to the host environment to be * executed by a command processor in an implementation-defined manner. * A null pointer may be used for string, to inquire whether a command * processor exists. * * Returns: If the argument is a null pointer, the system function returns * non-zero only if a command processor is available. If the * argument is not a null pointer, the system function returns an * implementation-defined value. */ extern _ARMABI_THROW void *bsearch(const void * /*key*/, const void * /*base*/, size_t /*nmemb*/, size_t /*size*/, int (* /*compar*/)(const void *, const void *)) __attribute__((__nonnull__(1,2,5))); /* * searches an array of nmemb objects, the initial member of which is * pointed to by base, for a member that matches the object pointed to by * key. The size of each member of the array is specified by size. * The contents of the array shall be in ascending sorted order according to * a comparison function pointed to by compar, which is called with two * arguments that point to the key object and to an array member, in that * order. The function shall return an integer less than, equal to, or * greater than zero if the key object is considered, respectively, to be * less than, to match, or to be greater than the array member. * Returns: a pointer to a matching member of the array, or a null pointer * if no match is found. If two members compare as equal, which * member is matched is unspecified. */ #if defined(__cplusplus) && defined(__LANGUAGE_LINKAGE_CHANGES_FUNCTION_TYPE) /* bsearch that takes a ptr to a function with C++ linkage * but not in GNU mode */ typedef int (* __C_compareprocptr)(const void *, const void *); extern "C++" void *bsearch(const void * __key, const void * __base, size_t __nmemb, size_t __size, int (* __compar)(const void *, const void *)) __attribute__((__nonnull__(1,2,5))); extern "C++" inline void *bsearch(const void * __key, const void * __base, size_t __nmemb, size_t __size, int (* __compar)(const void *, const void *)) { return bsearch(__key, __base, __nmemb, __size, (__C_compareprocptr)__compar); } #endif extern _ARMABI_THROW void qsort(void * /*base*/, size_t /*nmemb*/, size_t /*size*/, int (* /*compar*/)(const void *, const void *)) __attribute__((__nonnull__(1,4))); /* * sorts an array of nmemb objects, the initial member of which is pointed * to by base. The size of each object is specified by size. * The contents of the array shall be in ascending order according to a * comparison function pointed to by compar, which is called with two * arguments that point to the objects being compared. The function shall * return an integer less than, equal to, or greater than zero if the first * argument is considered to be respectively less than, equal to, or greater * than the second. If two members compare as equal, their order in the * sorted array is unspecified. */ #if defined(__cplusplus) && defined(__LANGUAGE_LINKAGE_CHANGES_FUNCTION_TYPE) /* qsort that takes a ptr to a function with C++ linkage * but not in GNU mode */ extern "C++" void qsort(void * __base, size_t __nmemb, size_t __size, int (* __compar)(const void *, const void *)) __attribute__((__nonnull__(1,4))); extern "C++" inline void qsort(void * __base, size_t __nmemb, size_t __size, int (* __compar)(const void *, const void *)) { qsort(__base, __nmemb, __size, (__C_compareprocptr)__compar); } #endif extern _ARMABI_PURE int abs(int /*j*/); /* * computes the absolute value of an integer j. If the result cannot be * represented, the behaviour is undefined. * Returns: the absolute value. */ extern _ARMABI_PURE div_t div(int /*numer*/, int /*denom*/); /* * computes the quotient and remainder of the division of the numerator * numer by the denominator denom. If the division is inexact, the resulting * quotient is the integer of lesser magnitude that is the nearest to the * algebraic quotient. If the result cannot be represented, the behaviour is * undefined; otherwise, quot * denom + rem shall equal numer. * Returns: a structure of type div_t, comprising both the quotient and the * remainder. the structure shall contain the following members, * in either order. * int quot; int rem; */ extern _ARMABI_PURE long int labs(long int /*j*/); /* * computes the absolute value of an long integer j. If the result cannot be * represented, the behaviour is undefined. * Returns: the absolute value. */ #ifdef __cplusplus extern "C++" inline _ARMABI_PURE long abs(long int x) { return labs(x); } #endif extern _ARMABI_PURE ldiv_t ldiv(long int /*numer*/, long int /*denom*/); /* * computes the quotient and remainder of the division of the numerator * numer by the denominator denom. If the division is inexact, the sign of * the resulting quotient is that of the algebraic quotient, and the * magnitude of the resulting quotient is the largest integer less than the * magnitude of the algebraic quotient. If the result cannot be represented, * the behaviour is undefined; otherwise, quot * denom + rem shall equal * numer. * Returns: a structure of type ldiv_t, comprising both the quotient and the * remainder. the structure shall contain the following members, * in either order. * long int quot; long int rem; */ #ifdef __cplusplus extern "C++" inline _ARMABI_PURE ldiv_t div(long int __numer, long int __denom) { return ldiv(__numer, __denom); } #endif #if !defined(__STRICT_ANSI__) || __USE_C99_STDLIB extern _ARMABI_PURE __LONGLONG llabs(__LONGLONG /*j*/); /* * computes the absolute value of a long long integer j. If the * result cannot be represented, the behaviour is undefined. * Returns: the absolute value. */ #ifdef __cplusplus extern "C++" inline _ARMABI_PURE __LONGLONG abs(__LONGLONG x) { return llabs(x); } #endif extern _ARMABI_PURE lldiv_t lldiv(__LONGLONG /*numer*/, __LONGLONG /*denom*/); /* * computes the quotient and remainder of the division of the numerator * numer by the denominator denom. If the division is inexact, the sign of * the resulting quotient is that of the algebraic quotient, and the * magnitude of the resulting quotient is the largest integer less than the * magnitude of the algebraic quotient. If the result cannot be represented, * the behaviour is undefined; otherwise, quot * denom + rem shall equal * numer. * Returns: a structure of type lldiv_t, comprising both the quotient and the * remainder. the structure shall contain the following members, * in either order. * long long quot; long long rem; */ #ifdef __cplusplus extern "C++" inline _ARMABI_PURE lldiv_t div(__LONGLONG __numer, __LONGLONG __denom) { return lldiv(__numer, __denom); } #endif #endif #if !(__ARM_NO_DEPRECATED_FUNCTIONS) /* * ARM real-time divide functions for guaranteed performance */ typedef struct __sdiv32by16 { int quot, rem; } __sdiv32by16; typedef struct __udiv32by16 { unsigned int quot, rem; } __udiv32by16; /* used int so that values return in separate regs, although 16-bit */ typedef struct __sdiv64by32 { int rem, quot; } __sdiv64by32; __value_in_regs extern _ARMABI_PURE __sdiv32by16 __rt_sdiv32by16( int /*numer*/, short int /*denom*/); /* * Signed divide: (16-bit quot), (16-bit rem) = (32-bit) / (16-bit) */ __value_in_regs extern _ARMABI_PURE __udiv32by16 __rt_udiv32by16( unsigned int /*numer*/, unsigned short /*denom*/); /* * Unsigned divide: (16-bit quot), (16-bit rem) = (32-bit) / (16-bit) */ __value_in_regs extern _ARMABI_PURE __sdiv64by32 __rt_sdiv64by32( int /*numer_h*/, unsigned int /*numer_l*/, int /*denom*/); /* * Signed divide: (32-bit quot), (32-bit rem) = (64-bit) / (32-bit) */ #endif /* * ARM floating-point mask/status function (for both hardfp and softfp) */ extern _ARMABI unsigned int __fp_status(unsigned int /*mask*/, unsigned int /*flags*/); /* * mask and flags are bit-fields which correspond directly to the * floating point status register in the FPE/FPA and fplib. * __fp_status returns the current value of the status register, * and also sets the writable bits of the word * (the exception control and flag bytes) to: * * new = (old & ~mask) ^ flags; */ #define __fpsr_IXE 0x100000 #define __fpsr_UFE 0x80000 #define __fpsr_OFE 0x40000 #define __fpsr_DZE 0x20000 #define __fpsr_IOE 0x10000 #define __fpsr_IXC 0x10 #define __fpsr_UFC 0x8 #define __fpsr_OFC 0x4 #define __fpsr_DZC 0x2 #define __fpsr_IOC 0x1 /* * Multibyte Character Functions. * The behaviour of the multibyte character functions is affected by the * LC_CTYPE category of the current locale. For a state-dependent encoding, * each function is placed into its initial state by a call for which its * character pointer argument, s, is a null pointer. Subsequent calls with s * as other than a null pointer cause the internal state of the function to be * altered as necessary. A call with s as a null pointer causes these functions * to return a nonzero value if encodings have state dependency, and a zero * otherwise. After the LC_CTYPE category is changed, the shift state of these * functions is indeterminate. */ extern _ARMABI int mblen(const char * /*s*/, size_t /*n*/); /* * If s is not a null pointer, the mblen function determines the number of * bytes compromising the multibyte character pointed to by s. Except that * the shift state of the mbtowc function is not affected, it is equivalent * to mbtowc((wchar_t *)0, s, n); * Returns: If s is a null pointer, the mblen function returns a nonzero or * zero value, if multibyte character encodings, respectively, do * or do not have state-dependent encodings. If s is not a null * pointer, the mblen function either returns a 0 (if s points to a * null character), or returns the number of bytes that compromise * the multibyte character (if the next n of fewer bytes form a * valid multibyte character), or returns -1 (they do not form a * valid multibyte character). */ extern _ARMABI int mbtowc(wchar_t * __restrict /*pwc*/, const char * __restrict /*s*/, size_t /*n*/); /* * If s is not a null pointer, the mbtowc function determines the number of * bytes that compromise the multibyte character pointed to by s. It then * determines the code for value of type wchar_t that corresponds to that * multibyte character. (The value of the code corresponding to the null * character is zero). If the multibyte character is valid and pwc is not a * null pointer, the mbtowc function stores the code in the object pointed * to by pwc. At most n bytes of the array pointed to by s will be examined. * Returns: If s is a null pointer, the mbtowc function returns a nonzero or * zero value, if multibyte character encodings, respectively, do * or do not have state-dependent encodings. If s is not a null * pointer, the mbtowc function either returns a 0 (if s points to * a null character), or returns the number of bytes that * compromise the converted multibyte character (if the next n of * fewer bytes form a valid multibyte character), or returns -1 * (they do not form a valid multibyte character). */ extern _ARMABI int wctomb(char * /*s*/, wchar_t /*wchar*/); /* * determines the number of bytes need to represent the multibyte character * corresponding to the code whose value is wchar (including any change in * shift state). It stores the multibyte character representation in the * array object pointed to by s (if s is not a null pointer). At most * MB_CUR_MAX characters are stored. If the value of wchar is zero, the * wctomb function is left in the initial shift state). * Returns: If s is a null pointer, the wctomb function returns a nonzero or * zero value, if multibyte character encodings, respectively, do * or do not have state-dependent encodings. If s is not a null * pointer, the wctomb function returns a -1 if the value of wchar * does not correspond to a valid multibyte character, or returns * the number of bytes that compromise the multibyte character * corresponding to the value of wchar. */ /* * Multibyte String Functions. * The behaviour of the multibyte string functions is affected by the LC_CTYPE * category of the current locale. */ extern _ARMABI size_t mbstowcs(wchar_t * __restrict /*pwcs*/, const char * __restrict /*s*/, size_t /*n*/) __attribute__((__nonnull__(2))); /* * converts a sequence of multibyte character that begins in the initial * shift state from the array pointed to by s into a sequence of * corresponding codes and stores not more than n codes into the array * pointed to by pwcs. No multibyte character that follow a null character * (which is converted into a code with value zero) will be examined or * converted. Each multibyte character is converted as if by a call to * mbtowc function, except that the shift state of the mbtowc function is * not affected. No more than n elements will be modified in the array * pointed to by pwcs. If copying takes place between objects that overlap, * the behaviour is undefined. * Returns: If an invalid multibyte character is encountered, the mbstowcs * function returns (size_t)-1. Otherwise, the mbstowcs function * returns the number of array elements modified, not including * a terminating zero code, if any. */ extern _ARMABI size_t wcstombs(char * __restrict /*s*/, const wchar_t * __restrict /*pwcs*/, size_t /*n*/) __attribute__((__nonnull__(2))); /* * converts a sequence of codes that correspond to multibyte characters * from the array pointed to by pwcs into a sequence of multibyte * characters that begins in the initial shift state and stores these * multibyte characters into the array pointed to by s, stopping if a * multibyte character would exceed the limit of n total bytes or if a * null character is stored. Each code is converted as if by a call to the * wctomb function, except that the shift state of the wctomb function is * not affected. No more than n elements will be modified in the array * pointed to by s. If copying takes place between objects that overlap, * the behaviour is undefined. * Returns: If a code is encountered that does not correspond to a valid * multibyte character, the wcstombs function returns (size_t)-1. * Otherwise, the wcstombs function returns the number of bytes * modified, not including a terminating null character, if any. */ extern _ARMABI void __use_realtime_heap(void); extern _ARMABI void __use_realtime_division(void); extern _ARMABI void __use_two_region_memory(void); extern _ARMABI void __use_no_heap(void); extern _ARMABI void __use_no_heap_region(void); extern _ARMABI char const *__C_library_version_string(void); extern _ARMABI int __C_library_version_number(void); #ifdef __cplusplus } /* extern "C" */ } /* namespace std */ #endif /* __cplusplus */ #endif /* __STDLIB_DECLS */ #if _AEABI_PORTABILITY_LEVEL != 0 && !defined _AEABI_PORTABLE #define _AEABI_PORTABLE #endif #ifdef __cplusplus #ifndef __STDLIB_NO_EXPORTS #if !defined(__STRICT_ANSI__) || __USE_C99_STDLIB using ::std::atoll; using ::std::lldiv_t; #endif /* !defined(__STRICT_ANSI__) || __USE_C99_STDLIB */ using ::std::div_t; using ::std::ldiv_t; using ::std::atof; using ::std::atoi; using ::std::atol; using ::std::strtod; #if !defined(__STRICT_ANSI__) || __USE_C99_STDLIB using ::std::strtof; using ::std::strtold; #endif using ::std::strtol; using ::std::strtoul; using ::std::strtoll; using ::std::strtoull; using ::std::rand; using ::std::srand; using ::std::_rand_state; using ::std::_rand_r; using ::std::_srand_r; using ::std::_ANSI_rand_state; using ::std::_ANSI_rand_r; using ::std::_ANSI_srand_r; using ::std::calloc; using ::std::free; using ::std::malloc; using ::std::realloc; #if !defined(__STRICT_ANSI__) using ::std::posix_memalign; #endif using ::std::__heapprt; using ::std::__heapstats; using ::std::__heapvalid; using ::std::abort; using ::std::atexit; using ::std::exit; using ::std::_Exit; using ::std::getenv; using ::std::system; using ::std::bsearch; using ::std::qsort; using ::std::abs; using ::std::div; using ::std::labs; using ::std::ldiv; #if !defined(__STRICT_ANSI__) || __USE_C99_STDLIB using ::std::llabs; using ::std::lldiv; #endif /* !defined(__STRICT_ANSI__) || __USE_C99_STDLIB */ #if !(__ARM_NO_DEPRECATED_FUNCTIONS) using ::std::__sdiv32by16; using ::std::__udiv32by16; using ::std::__sdiv64by32; using ::std::__rt_sdiv32by16; using ::std::__rt_udiv32by16; using ::std::__rt_sdiv64by32; #endif using ::std::__fp_status; using ::std::mblen; using ::std::mbtowc; using ::std::wctomb; using ::std::mbstowcs; using ::std::wcstombs; using ::std::__use_realtime_heap; using ::std::__use_realtime_division; using ::std::__use_two_region_memory; using ::std::__use_no_heap; using ::std::__use_no_heap_region; using ::std::__C_library_version_string; using ::std::__C_library_version_number; using ::std::size_t; using ::std::__aeabi_MB_CUR_MAX; #endif /* __STDLIB_NO_EXPORTS */ #endif /* __cplusplus */ #undef __LONGLONG #endif /* __stdlib_h */ /* end of stdlib.h */ 这是啥
07-09
参考我给你的图像上传代码和下面的图像识别并在oled上显示的代码,将他们整合在一起/* Edge Impulse Arduino examples * Copyright (c) 2022 EdgeImpulse Inc. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ // These sketches are tested with 2.0.4 ESP32 Arduino Core // https://github.com/espressif/arduino-esp32/releases/tag/2.0.4 /* Includes ---------------------------------------------------------------- */ #include <shibie_inferencing.h> #include "edge-impulse-sdk/dsp/image/image.hpp" #include "esp_camera.h" // Select camera model - find more camera models in camera_pins.h file here // https://github.com/espressif/arduino-esp32/blob/master/libraries/ESP32/examples/Camera/CameraWebServer/camera_pins.h //#define CAMERA_MODEL_ESP_EYE // Has PSRAM #define CAMERA_MODEL_AI_THINKER // Has PSRAM #if defined(CAMERA_MODEL_ESP_EYE) #define PWDN_GPIO_NUM -1 #define RESET_GPIO_NUM -1 #define XCLK_GPIO_NUM 4 #define SIOD_GPIO_NUM 18 #define SIOC_GPIO_NUM 23 #define Y9_GPIO_NUM 36 #define Y8_GPIO_NUM 37 #define Y7_GPIO_NUM 38 #define Y6_GPIO_NUM 39 #define Y5_GPIO_NUM 35 #define Y4_GPIO_NUM 14 #define Y3_GPIO_NUM 13 #define Y2_GPIO_NUM 34 #define VSYNC_GPIO_NUM 5 #define HREF_GPIO_NUM 27 #define PCLK_GPIO_NUM 25 #elif defined(CAMERA_MODEL_AI_THINKER) #define PWDN_GPIO_NUM 32 #define RESET_GPIO_NUM -1 #define XCLK_GPIO_NUM 0 #define SIOD_GPIO_NUM 26 #define SIOC_GPIO_NUM 27 #define Y9_GPIO_NUM 35 #define Y8_GPIO_NUM 34 #define Y7_GPIO_NUM 39 #define Y6_GPIO_NUM 36 #define Y5_GPIO_NUM 21 #define Y4_GPIO_NUM 19 #define Y3_GPIO_NUM 18 #define Y2_GPIO_NUM 5 #define VSYNC_GPIO_NUM 25 #define HREF_GPIO_NUM 23 #define PCLK_GPIO_NUM 22 #else #error "Camera model not selected" #endif /* Constant defines -------------------------------------------------------- */ #define EI_CAMERA_RAW_FRAME_BUFFER_COLS 320 #define EI_CAMERA_RAW_FRAME_BUFFER_ROWS 240 #define EI_CAMERA_FRAME_BYTE_SIZE 3 /* Private variables ------------------------------------------------------- */ static bool debug_nn = false; // Set this to true to see e.g. features generated from the raw signal static bool is_initialised = false; uint8_t *snapshot_buf; //points to the output of the capture static camera_config_t camera_config = { .pin_pwdn = PWDN_GPIO_NUM, .pin_reset = RESET_GPIO_NUM, .pin_xclk = XCLK_GPIO_NUM, .pin_sscb_sda = SIOD_GPIO_NUM, .pin_sscb_scl = SIOC_GPIO_NUM, .pin_d7 = Y9_GPIO_NUM, .pin_d6 = Y8_GPIO_NUM, .pin_d5 = Y7_GPIO_NUM, .pin_d4 = Y6_GPIO_NUM, .pin_d3 = Y5_GPIO_NUM, .pin_d2 = Y4_GPIO_NUM, .pin_d1 = Y3_GPIO_NUM, .pin_d0 = Y2_GPIO_NUM, .pin_vsync = VSYNC_GPIO_NUM, .pin_href = HREF_GPIO_NUM, .pin_pclk = PCLK_GPIO_NUM, //XCLK 20MHz or 10MHz for OV2640 double FPS (Experimental) .xclk_freq_hz = 20000000, .ledc_timer = LEDC_TIMER_0, .ledc_channel = LEDC_CHANNEL_0, .pixel_format = PIXFORMAT_JPEG, //YUV422,GRAYSCALE,RGB565,JPEG .frame_size = FRAMESIZE_QVGA, //QQVGA-UXGA Do not use sizes above QVGA when not JPEG .jpeg_quality = 12, //0-63 lower number means higher quality .fb_count = 1, //if more than one, i2s runs in continuous mode. Use only with JPEG .fb_location = CAMERA_FB_IN_PSRAM, .grab_mode = CAMERA_GRAB_WHEN_EMPTY, }; /* Function definitions ------------------------------------------------------- */ bool ei_camera_init(void); void ei_camera_deinit(void); bool ei_camera_capture(uint32_t img_width, uint32_t img_height, uint8_t *out_buf) ; /** * @brief Arduino setup function */ void setup() { // put your setup code here, to run once: Serial.begin(115200); //comment out the below line to start inference immediately after upload while (!Serial); Serial.println("Edge Impulse Inferencing Demo"); if (ei_camera_init() == false) { ei_printf("Failed to initialize Camera!\r\n"); } else { ei_printf("Camera initialized\r\n"); } ei_printf("\nStarting continious inference in 2 seconds...\n"); ei_sleep(2000); } /** * @brief Get data and run inferencing * * @param[in] debug Get debug info if true */ void loop() { // instead of wait_ms, we'll wait on the signal, this allows threads to cancel us... if (ei_sleep(5) != EI_IMPULSE_OK) { return; } snapshot_buf = (uint8_t*)malloc(EI_CAMERA_RAW_FRAME_BUFFER_COLS * EI_CAMERA_RAW_FRAME_BUFFER_ROWS * EI_CAMERA_FRAME_BYTE_SIZE); // check if allocation was successful if(snapshot_buf == nullptr) { ei_printf("ERR: Failed to allocate snapshot buffer!\n"); return; } ei::signal_t signal; signal.total_length = EI_CLASSIFIER_INPUT_WIDTH * EI_CLASSIFIER_INPUT_HEIGHT; signal.get_data = &ei_camera_get_data; if (ei_camera_capture((size_t)EI_CLASSIFIER_INPUT_WIDTH, (size_t)EI_CLASSIFIER_INPUT_HEIGHT, snapshot_buf) == false) { ei_printf("Failed to capture image\r\n"); free(snapshot_buf); return; } // Run the classifier ei_impulse_result_t result = { 0 }; EI_IMPULSE_ERROR err = run_classifier(&signal, &result, debug_nn); if (err != EI_IMPULSE_OK) { ei_printf("ERR: Failed to run classifier (%d)\n", err); return; } // print the predictions ei_printf("Predictions (DSP: %d ms., Classification: %d ms., Anomaly: %d ms.): \n", result.timing.dsp, result.timing.classification, result.timing.anomaly); #if EI_CLASSIFIER_OBJECT_DETECTION == 1 ei_printf("Object detection bounding boxes:\r\n"); for (uint32_t i = 0; i < result.bounding_boxes_count; i++) { ei_impulse_result_bounding_box_t bb = result.bounding_boxes[i]; if (bb.value == 0) { continue; } ei_printf(" %s (%f) [ x: %u, y: %u, width: %u, height: %u ]\r\n", bb.label, bb.value, bb.x, bb.y, bb.width, bb.height); } // Print the prediction results (classification) #else ei_printf("Predictions:\r\n"); for (uint16_t i = 0; i < EI_CLASSIFIER_LABEL_COUNT; i++) { ei_printf(" %s: ", ei_classifier_inferencing_categories[i]); ei_printf("%.5f\r\n", result.classification[i].value); } #endif // Print anomaly result (if it exists) #if EI_CLASSIFIER_HAS_ANOMALY ei_printf("Anomaly prediction: %.3f\r\n", result.anomaly); #endif #if EI_CLASSIFIER_HAS_VISUAL_ANOMALY ei_printf("Visual anomalies:\r\n"); for (uint32_t i = 0; i < result.visual_ad_count; i++) { ei_impulse_result_bounding_box_t bb = result.visual_ad_grid_cells[i]; if (bb.value == 0) { continue; } ei_printf(" %s (%f) [ x: %u, y: %u, width: %u, height: %u ]\r\n", bb.label, bb.value, bb.x, bb.y, bb.width, bb.height); } #endif free(snapshot_buf); } /** * @brief Setup image sensor & start streaming * * @retval false if initialisation failed */ bool ei_camera_init(void) { if (is_initialised) return true; #if defined(CAMERA_MODEL_ESP_EYE) pinMode(13, INPUT_PULLUP); pinMode(14, INPUT_PULLUP); #endif //initialize the camera esp_err_t err = esp_camera_init(&camera_config); if (err != ESP_OK) { Serial.printf("Camera init failed with error 0x%x\n", err); return false; } sensor_t * s = esp_camera_sensor_get(); // initial sensors are flipped vertically and colors are a bit saturated if (s->id.PID == OV3660_PID) { s->set_vflip(s, 1); // flip it back s->set_brightness(s, 1); // up the brightness just a bit s->set_saturation(s, 0); // lower the saturation } #if defined(CAMERA_MODEL_M5STACK_WIDE) s->set_vflip(s, 1); s->set_hmirror(s, 1); #elif defined(CAMERA_MODEL_ESP_EYE) s->set_vflip(s, 1); s->set_hmirror(s, 1); s->set_awb_gain(s, 1); #endif is_initialised = true; return true; } /** * @brief Stop streaming of sensor data */ void ei_camera_deinit(void) { //deinitialize the camera esp_err_t err = esp_camera_deinit(); if (err != ESP_OK) { ei_printf("Camera deinit failed\n"); return; } is_initialised = false; return; } /** * @brief Capture, rescale and crop image * * @param[in] img_width width of output image * @param[in] img_height height of output image * @param[in] out_buf pointer to store output image, NULL may be used * if ei_camera_frame_buffer is to be used for capture and resize/cropping. * * @retval false if not initialised, image captured, rescaled or cropped failed * */ bool ei_camera_capture(uint32_t img_width, uint32_t img_height, uint8_t *out_buf) { bool do_resize = false; if (!is_initialised) { ei_printf("ERR: Camera is not initialized\r\n"); return false; } camera_fb_t *fb = esp_camera_fb_get(); if (!fb) { ei_printf("Camera capture failed\n"); return false; } bool converted = fmt2rgb888(fb->buf, fb->len, PIXFORMAT_JPEG, snapshot_buf); esp_camera_fb_return(fb); if(!converted){ ei_printf("Conversion failed\n"); return false; } if ((img_width != EI_CAMERA_RAW_FRAME_BUFFER_COLS) || (img_height != EI_CAMERA_RAW_FRAME_BUFFER_ROWS)) { do_resize = true; } if (do_resize) { ei::image::processing::crop_and_interpolate_rgb888( out_buf, EI_CAMERA_RAW_FRAME_BUFFER_COLS, EI_CAMERA_RAW_FRAME_BUFFER_ROWS, out_buf, img_width, img_height); } return true; } static int ei_camera_get_data(size_t offset, size_t length, float *out_ptr) { // we already have a RGB888 buffer, so recalculate offset into pixel index size_t pixel_ix = offset * 3; size_t pixels_left = length; size_t out_ptr_ix = 0; while (pixels_left != 0) { // Swap BGR to RGB here // due to https://github.com/espressif/esp32-camera/issues/379 out_ptr[out_ptr_ix] = (snapshot_buf[pixel_ix + 2] << 16) + (snapshot_buf[pixel_ix + 1] << 8) + snapshot_buf[pixel_ix]; // go to the next pixel out_ptr_ix++; pixel_ix+=3; pixels_left--; } // and done! return 0; } #if !defined(EI_CLASSIFIER_SENSOR) || EI_CLASSIFIER_SENSOR != EI_CLASSIFIER_SENSOR_CAMERA使用下面的头文件#include <WiFi.h> #include "esp_camera.h" #include <shibie_inferencing.h> #include "edge-impulse-sdk/dsp/image/image.hpp" #include "freertos/semphr.h" // 互斥锁头文件 #include "esp_task_wdt.h" #include "freertos/task.h" #include "esp_http_server.h" #error "Invalid model for current sensor" #endif
最新发布
07-22
#include <assert.h> #include "soapH.h" //#include "wsdd.nsmap" #include "soapStub.h" #include "wsseapi.h" #include "wsaapi.h" #include <map> #define USERNAME "admin" #define PASSWORD "admin888" #define MEDIA_GET_PROFILES_ACTION "http://www.onvif.org/ver10/media/wsdl/GetProfiles". #define SOAP_ASSERT assert #define SOAP_DBGLOG printf #define SOAP_DBGERR printf #define SOAP_TO "urn:schemas-xmlsoap-org:ws:2005:04:discovery" #define SOAP_ACTION "http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe" #define SOAP_MCAST_ADDR "soap.udp://239.255.255.250:3702" // onvif规定的组播地址 #define SOAP_ITEM "" // 寻找的设备范围 #define SOAP_TYPES "dn:NetworkVideoTransmitter" // 寻找的设备类型 #define SOAP_SOCK_TIMEOUT (3) // socket超时时间(单秒秒) #define ONVIF_ADDRESS_SIZE (128) // URI地址长度 #define ONVIF_TOKEN_SIZE (65) // token长度 #define SOAP_CHECK_ERROR(result, soap, str) \ do { \ if (SOAP_OK != (result) || SOAP_OK != (soap)->error) { \ soap_perror((soap), (str)); \ if (SOAP_OK == (result)) { \ (result) = (soap)->error; \ } \ goto EXIT; \ } \ } while (0) /* 视频编码器配置信息 */ struct tagVideoEncoderConfiguration { char token[ONVIF_TOKEN_SIZE]; // 唯一标识该视频编码器的令牌字符串 int Width; // 分辨率 int Height; }; /* 设备配置信息 */ struct tagProfile { char token[ONVIF_TOKEN_SIZE]; // 唯一标识设备配置文件的令牌字符串 struct tagVideoEncoderConfiguration venc; // 视频编码器配置信息 }; /* 设备能力信息 */ struct tagCapabilities { char MediaXAddr[ONVIF_ADDRESS_SIZE]; // 媒体服务地址 char EventXAddr[ONVIF_ADDRESS_SIZE]; // 事件服务地址 }; void soap_perror(struct soap* soap, const char* str) { if (nullptr == str) { SOAP_DBGERR("[soap] error: %d, %s, %s\n", soap->error, *soap_faultcode(soap), *soap_faultstring(soap)); } else { SOAP_DBGERR("[soap] %s error: %d, %s, %s\n", str, soap->error, *soap_faultcode(soap), *soap_faultstring(soap)); } } void* ONVIF_soap_malloc(struct soap* soap, unsigned int n) { void* p = nullptr; if (n > 0) { p = soap_malloc(soap, n); SOAP_ASSERT(nullptr != p); memset(p, 0x00, n); } return p; } struct soap* ONVIF_soap_new(int timeout) { struct soap* soap = nullptr; // soap环境变量 SOAP_ASSERT(nullptr != (soap = soap_new())); soap_set_namespaces(soap, namespaces); // 设置soap的namespaces soap->recv_timeout = timeout; // 设置超时(超过指定时间没有数据就退出) soap->send_timeout = timeout; soap->connect_timeout = timeout; #if defined(__linux__) || defined(__linux) // 参考https://www.genivia.com/dev.html#client-c的修改: soap->socket_flags = MSG_NOSIGNAL; // To prevent connection reset errors #endif soap_set_mode(soap, SOAP_C_UTFSTRING); // 设置为UTF-8编码,否则叠加中文OSD会乱码 return soap; } void ONVIF_soap_delete(struct soap* soap) { soap_destroy(soap); // remove deserialized class instances (C++ only) soap_end(soap); // Clean up deserialized data (except class instances) and temporary data soap_done(soap); // Reset, close communications, and remove callbacks soap_free(soap); // Reset and deallocate the context created with soap_new or soap_copy } /************************************************************************ **函数:ONVIF_init_header **功能:初始化soap描述消息头 **参数: [in] soap - soap环境变量 **返回:无 **备注: 1). 在本函数内部通过ONVIF_soap_malloc分配的内存,将在ONVIF_soap_delete中被释放 ************************************************************************/ void ONVIF_init_header(struct soap* soap) { struct SOAP_ENV__Header* header = nullptr; SOAP_ASSERT(nullptr != soap); header = (struct SOAP_ENV__Header*)ONVIF_soap_malloc(soap, sizeof(struct SOAP_ENV__Header)); soap_default_SOAP_ENV__Header(soap, header); header->wsa__MessageID = (char*)soap_wsa_rand_uuid(soap); header->wsa__To = (char*)ONVIF_soap_malloc(soap, strlen(SOAP_TO) + 1); header->wsa__Action = (char*)ONVIF_soap_malloc(soap, strlen(SOAP_ACTION) + 1); strcpy(header->wsa__To, SOAP_TO); strcpy(header->wsa__Action, SOAP_ACTION); soap->header = header; return; } /************************************************************************ **函数:ONVIF_init_ProbeType **功能:初始化探测设备的范围和类型 **参数: [in] soap - soap环境变量 [out] probe - 填充要探测的设备范围和类型 **返回: 0表明探测到,非0表明未探测到 **备注: 1). 在本函数内部通过ONVIF_soap_malloc分配的内存,将在ONVIF_soap_delete中被释放 ************************************************************************/ void ONVIF_init_ProbeType(struct soap* soap, struct wsdd__ProbeType* probe) { struct wsdd__ScopesType* scope = nullptr; // 用于描述查找哪类的Web服务 SOAP_ASSERT(nullptr != soap); SOAP_ASSERT(nullptr != probe); scope = (struct wsdd__ScopesType*)ONVIF_soap_malloc(soap, sizeof(struct wsdd__ScopesType)); soap_default_wsdd__ScopesType(soap, scope); // 设置寻找设备的范围 scope->__item = (char*)ONVIF_soap_malloc(soap, strlen(SOAP_ITEM) + 1); strcpy(scope->__item, SOAP_ITEM); memset(probe, 0x00, sizeof(struct wsdd__ProbeType)); soap_default_wsdd__ProbeType(soap, probe); probe->Scopes = scope; probe->Types = (char*)ONVIF_soap_malloc(soap, strlen(SOAP_TYPES) + 1); // 设置寻找设备的类型 strcpy(probe->Types, SOAP_TYPES); return; } void ONVIF_DetectDevice(void (*cb)(char* DeviceXAddr)) { int i; int result = 0; unsigned int count = 0; // 搜索到的设备个数 struct soap* soap = nullptr; // soap环境变量 struct wsdd__ProbeType req; // 用于发送Probe消息 struct __wsdd__ProbeMatches rep; // 用于接收Probe应答 struct wsdd__ProbeMatchType* probeMatch; SOAP_DBGLOG("Sending Probe message to multicast address...\n"); SOAP_ASSERT(nullptr != (soap = ONVIF_soap_new(SOAP_SOCK_TIMEOUT))); ONVIF_init_header(soap); // 设置消息头描述 ONVIF_init_ProbeType(soap, &req); // 设置寻找的设备的范围和类型 result = soap_send___wsdd__Probe(soap, SOAP_MCAST_ADDR, nullptr, &req); // 向组播地址广播Probe消息 while (SOAP_OK == result) // 开始循环接收设备发送过来的消息 { memset(&rep, 0x00, sizeof(rep)); result = soap_recv___wsdd__ProbeMatches(soap, &rep); if (SOAP_OK == result) { if (soap->error) { soap_perror(soap, "ProbeMatches"); } else { // 成功接收到设备的应答消息 if (nullptr != rep.wsdd__ProbeMatches) { count += rep.wsdd__ProbeMatches->__sizeProbeMatch; SOAP_DBGLOG("成功接收...\n"); for (i = 0; i < rep.wsdd__ProbeMatches->__sizeProbeMatch; i++) { probeMatch = rep.wsdd__ProbeMatches->ProbeMatch + i; if (nullptr != cb) { cb(probeMatch->XAddrs); // 使用设备服务地址执行函数回调 } } } } } else if (soap->error) { break; } } SOAP_DBGLOG("\ndetect end! It has detected %d devices!\n", count); if (nullptr != soap) { ONVIF_soap_delete(soap); } } /************************************************************************ **函数:ONVIF_SetAuthInfo **功能:设置认证信息 **参数: [in] soap - soap环境变量 [in] username - 用户名 [in] password - 密码 **返回: 0表明成功,非0表明失败 **备注: ************************************************************************/ static int ONVIF_SetAuthInfo(struct soap* soap, const char* username, const char* password) { int result = 0; SOAP_ASSERT(nullptr != USERNAME); SOAP_ASSERT(nullptr != PASSWORD); result = soap_wsse_add_UsernameTokenDigest(soap, nullptr, username, password); SOAP_CHECK_ERROR(result, soap, "add_UsernameTokenDigest"); EXIT: return result; } /************************************************************************ **函数:make_uri_withauth **功能:构造带有认证信息的URI地址 **参数: [in] src_uri - 未带认证信息的URI地址 [in] username - 用户名 [in] password - 密码 [out] dest_uri - 返回的带认证信息的URI地址 [in] size_dest_uri - dest_uri缓存大小 **返回: 0成功,非0失败 **备注: 1). 例子: 无认证信息的uri:rtsp://100.100.100.140:554/av0_0 带认证信息的uri:rtsp://username:password@100.100.100.140:554/av0_0 ************************************************************************/ static int make_uri_withauth(const std::string& src_uri, const std::string& username, const std::string& password, std::string* dest_uri) { int result = 0; unsigned int needBufSize = 0; SOAP_ASSERT(!src_uri.empty()); if (username.empty() && password.empty()) { // 生成新的uri地址 *dest_uri = src_uri; } else { std::string::size_type position = src_uri.find("//"); if (std::string::npos == position) { SOAP_DBGERR("can't found '//', src uri is: %s.\n", src_uri.c_str()); result = -1; return result; } position += 2; dest_uri->append(src_uri, 0, position); dest_uri->append(username + ":" + password + "@"); dest_uri->append(src_uri, position, std::string::npos); } return result; } /************************************************************************ **函数:ONVIF_GetDeviceInformation **功能:获取设备基本信息 **参数: [in] DeviceXAddr - 设备服务地址 **返回: 0表明成功,非0表明失败 **备注: ************************************************************************/ int ONVIF_GetDeviceInformation(const char* DeviceXAddr) { int result = 0; struct soap* soap = nullptr; _tds__GetDeviceInformation devinfo_req; _tds__GetDeviceInformationResponse devinfo_resp; SOAP_ASSERT(nullptr != DeviceXAddr); SOAP_ASSERT(nullptr != (soap = ONVIF_soap_new(SOAP_SOCK_TIMEOUT))); ONVIF_SetAuthInfo(soap, USERNAME, PASSWORD); result = soap_call___tds__GetDeviceInformation(soap, DeviceXAddr, nullptr, &devinfo_req, devinfo_resp); SOAP_CHECK_ERROR(result, soap, "GetDeviceInformation"); std::cout << " Manufacturer:\t" << devinfo_resp.Manufacturer << "\n"; std::cout << " Model:\t" << devinfo_resp.Model << "\n"; std::cout << " FirmwareVersion:\t" << devinfo_resp.FirmwareVersion << "\n"; std::cout << " SerialNumber:\t" << devinfo_resp.SerialNumber << "\n"; std::cout << " HardwareId:\t" << devinfo_resp.HardwareId << "\n"; EXIT: if (nullptr != soap) { ONVIF_soap_delete(soap); } return result; } /************************************************************************ **函数:ONVIF_GetCapabilities **功能:获取设备能力信息 **参数: [in] DeviceXAddr - 设备服务地址 [in] **返回: 0表明成功,非0表明失败 **备注: 1). 其中最主要的参数之一是媒体服务地址 ************************************************************************/ int ONVIF_GetCapabilities(const std::string& deviceXAddr, std::string* ptzXAddr) { int result = 0; struct soap* soap = nullptr; _tds__GetCapabilities devinfo_req; _tds__GetCapabilitiesResponse devinfo_resp; SOAP_ASSERT(!deviceXAddr.empty()); SOAP_ASSERT(nullptr != (soap = ONVIF_soap_new(SOAP_SOCK_TIMEOUT))); ONVIF_SetAuthInfo(soap, USERNAME, PASSWORD); result = soap_call___tds__GetCapabilities(soap, deviceXAddr.c_str(), nullptr, &devinfo_req, devinfo_resp); SOAP_CHECK_ERROR(result, soap, "GetCapabilities"); if (devinfo_resp.Capabilities->PTZ != nullptr) { *ptzXAddr = devinfo_resp.Capabilities->PTZ->XAddr; } EXIT: if (nullptr != soap) { ONVIF_soap_delete(soap); } return result; } int ONVIF_GetProfiles(const std::string& ptzXAddr, std::string* profilesToken) { int result = 0; struct soap* soap = nullptr; _trt__GetProfiles devinfo_req; _trt__GetProfilesResponse devinfo_resp; SOAP_ASSERT(!ptzXAddr.empty()); SOAP_ASSERT(nullptr != (soap = ONVIF_soap_new(SOAP_SOCK_TIMEOUT))); ONVIF_SetAuthInfo(soap, USERNAME, PASSWORD); result = soap_call___trt__GetProfiles(soap, ptzXAddr.c_str(), nullptr, &devinfo_req, devinfo_resp); SOAP_CHECK_ERROR(result, soap, "ONVIF_GetProfiles"); SOAP_ASSERT(!devinfo_resp.Profiles.empty()); *profilesToken = devinfo_resp.Profiles[0]->token; EXIT: if (nullptr != soap) { ONVIF_soap_delete(soap); } return result; } /************************************************************************ **函数:ONVIF_GetSnapshotUri **功能:获取设备图像抓拍地址(HTTP) **参数: [in] MediaXAddr - 媒体服务地址 [in] ProfileToken - the media profile token [out] uri - 返回的地址 [in] sizeuri - 地址缓存大小 **返回: 0表明成功,非0表明失败 **备注: 1). 并非所有的ProfileToken都支持图像抓拍地址。举例:XXX品牌的IPC有如下三个配置profile0/profile1/TestMediaProfile,其中TestMediaProfile返回的图像抓拍地址就是空指针。 ************************************************************************/ int ONVIF_GetSnapshotUri(const std::string& MediaXAddr, const std::string& ProfileToken, std::string* snapUri) { int result = 0; struct soap* soap = nullptr; _trt__GetSnapshotUri req; _trt__GetSnapshotUriResponse rep; // 检查参数合法性 if (MediaXAddr.empty() || ProfileToken.empty()) { SOAP_DBGERR("Invalid arguments\n"); return -1; } // 创建 SOAP 环境 soap = ONVIF_soap_new(SOAP_SOCK_TIMEOUT); if (!soap) { SOAP_DBGERR("Failed to create SOAP environment\n"); return -1; } // 设置认证信息 if (ONVIF_SetAuthInfo(soap, USERNAME, PASSWORD) != 0) { SOAP_DBGERR("Failed to set authentication info\n"); goto EXIT; } // 设置请求参数 memset(&req, 0, sizeof(req)); req.ProfileToken = soap_strdup(soap, ProfileToken.c_str()); // 调用服务 result = soap_call___trt__GetSnapshotUri(soap, MediaXAddr.c_str(), NULL, &req, rep); if (result != SOAP_OK || soap->error != SOAP_OK) { SOAP_DBGERR("GetSnapshotUri failed: %d, %s\n", soap->error, *soap_faultstring(soap)); result = -1; goto EXIT; } // 处理响应 - 使用正确的成员访问方式 if (rep.MediaUri) { *snapUri = rep.MediaUri->Uri; // Uri 是 std::string 类型,不需要指针检查 SOAP_DBGLOG("Snapshot URI: %s\n", snapUri->c_str()); } else { SOAP_DBGERR("MediaUri is null\n"); result = -1; } EXIT: // 清理资源 if (soap) { ONVIF_soap_delete(soap); } return result; } // 获取当前ptz的位置以及状态 int ONVIF_PTZ_GetStatus(const std::string& ptzXAddr, const std::string& ProfileToken) { int result = 0; struct soap* soap = nullptr; _tptz__GetStatus getStatus; _tptz__GetStatusResponse getStatusResponse; SOAP_ASSERT(!ptzXAddr.empty()); SOAP_ASSERT(nullptr != (soap = ONVIF_soap_new(SOAP_SOCK_TIMEOUT))); ONVIF_SetAuthInfo(soap, USERNAME, PASSWORD); getStatus.ProfileToken = const_cast<char*>(ProfileToken.c_str()); result = soap_call___tptz__GetStatus(soap, ptzXAddr.c_str(), nullptr, &getStatus, getStatusResponse); SOAP_CHECK_ERROR(result, soap, "ONVIF_PTZ_GetStatus"); if (*getStatusResponse.PTZStatus->MoveStatus->PanTilt == tt__MoveStatus__IDLE) { std::cout << " 空闲 ... " << std::endl; } else if (*getStatusResponse.PTZStatus->MoveStatus->PanTilt == tt__MoveStatus__MOVING) { std::cout << " 移动中 ... " << std::endl; } else if (*getStatusResponse.PTZStatus->MoveStatus->PanTilt == tt__MoveStatus__UNKNOWN) { std::cout << " 未知 ... " << std::endl; } std::cout << "当前p: " << getStatusResponse.PTZStatus->Position->PanTilt->x << "\n"; std::cout << "当前t: " << getStatusResponse.PTZStatus->Position->PanTilt->y << "\n"; std::cout << "当前z: " << getStatusResponse.PTZStatus->Position->Zoom->x << "\n"; EXIT: if (nullptr != soap) { ONVIF_soap_delete(soap); } return 0; } // 以指定速度移动到指定位置的ptz // p : -1 ~ 1 [] // t : -1 ~ 1 // z : 0 ~ 1 int ONVIF_PTZAbsoluteMove(const std::string& ptzXAddr, const std::string& ProfileToken) { int result = 0; struct soap* soap = nullptr; _tptz__AbsoluteMove absoluteMove; _tptz__AbsoluteMoveResponse absoluteMoveResponse; SOAP_ASSERT(!ptzXAddr.empty() && !ProfileToken.empty()); SOAP_ASSERT(nullptr != (soap = ONVIF_soap_new(SOAP_SOCK_TIMEOUT))); ONVIF_SetAuthInfo(soap, USERNAME, PASSWORD); absoluteMove.ProfileToken = const_cast<char*>(ProfileToken.c_str()); absoluteMove.Position = soap_new_tt__PTZVector(soap); absoluteMove.Position->PanTilt = soap_new_tt__Vector2D(soap); absoluteMove.Position->Zoom = soap_new_tt__Vector1D(soap); absoluteMove.Speed = soap_new_tt__PTZSpeed(soap); absoluteMove.Speed->PanTilt = soap_new_tt__Vector2D(soap); absoluteMove.Speed->Zoom = soap_new_tt__Vector1D(soap); absoluteMove.Position->PanTilt->x = 0.440833; // p absoluteMove.Position->PanTilt->y = 0.583455; // t absoluteMove.Position->Zoom->x = 0.0333333; // z // x 和y的绝对值越接近1,表示云台的速度越快 absoluteMove.Speed->PanTilt->x = 0.5; absoluteMove.Speed->PanTilt->y = 0.5; absoluteMove.Speed->Zoom->x = 0.5; result = soap_call___tptz__AbsoluteMove(soap, ptzXAddr.c_str(), nullptr, &absoluteMove, absoluteMoveResponse); SOAP_CHECK_ERROR(result, soap, "ONVIF_PTZAbsoluteMove"); EXIT: if (nullptr != soap) { ONVIF_soap_delete(soap); } return 0; } int ONVIF_PTZStopMove(const std::string& ptzXAddr, const std::string& ProfileToken) { int result = 0; struct soap* soap = nullptr; _tptz__Stop tptzStop; _tptz__StopResponse tptzStopResponse; SOAP_ASSERT(!ptzXAddr.empty() && !ProfileToken.empty()); SOAP_ASSERT(nullptr != (soap = ONVIF_soap_new(SOAP_SOCK_TIMEOUT))); ONVIF_SetAuthInfo(soap, USERNAME, PASSWORD); tptzStop.ProfileToken = const_cast<char*>(ProfileToken.c_str()); result = soap_call___tptz__Stop(soap, ptzXAddr.c_str(), nullptr, &tptzStop, tptzStopResponse); SOAP_CHECK_ERROR(result, soap, "ONVIF_PTZStopMove"); EXIT: if (nullptr != soap) { ONVIF_soap_delete(soap); } return result; } enum PTZCMD { PTZ_CMD_LEFT, PTZ_CMD_RIGHT, PTZ_CMD_UP, PTZ_CMD_DOWN, PTZ_CMD_LEFTUP, PTZ_CMD_LEFTDOWN, PTZ_CMD_RIGHTUP, PTZ_CMD_RIGHTDOWN, PTZ_CMD_ZOOM_IN, PTZ_CMD_ZOOM_OUT, }; // speed --> (0, 1] int ONVIF_PTZContinuousMove(const std::string& ptzXAddr, const std::string& ProfileToken, enum PTZCMD cmd, float speed) { int result = 0; struct soap* soap = nullptr; _tptz__ContinuousMove continuousMove; _tptz__ContinuousMoveResponse continuousMoveResponse; SOAP_ASSERT(!ptzXAddr.empty() && !ProfileToken.empty()); SOAP_ASSERT(nullptr != (soap = ONVIF_soap_new(SOAP_SOCK_TIMEOUT))); ONVIF_SetAuthInfo(soap, USERNAME, PASSWORD); continuousMove.ProfileToken = const_cast<char*>(ProfileToken.c_str()); continuousMove.Velocity = soap_new_tt__PTZSpeed(soap); continuousMove.Velocity->PanTilt = soap_new_tt__Vector2D(soap); continuousMove.Velocity->Zoom = soap_new_tt__Vector1D(soap); switch (cmd) { case PTZ_CMD_LEFT: continuousMove.Velocity->PanTilt->x = -speed; continuousMove.Velocity->PanTilt->y = 0; break; case PTZ_CMD_RIGHT: continuousMove.Velocity->PanTilt->x = speed; continuousMove.Velocity->PanTilt->y = 0; break; case PTZ_CMD_UP: continuousMove.Velocity->PanTilt->x = 0; continuousMove.Velocity->PanTilt->y = speed; break; case PTZ_CMD_DOWN: continuousMove.Velocity->PanTilt->x = 0; continuousMove.Velocity->PanTilt->y = -speed; break; case PTZ_CMD_LEFTUP: continuousMove.Velocity->PanTilt->x = -speed; continuousMove.Velocity->PanTilt->y = speed; break; case PTZ_CMD_LEFTDOWN: continuousMove.Velocity->PanTilt->x = -speed; continuousMove.Velocity->PanTilt->y = -speed; break; case PTZ_CMD_RIGHTUP: continuousMove.Velocity->PanTilt->x = speed; continuousMove.Velocity->PanTilt->y = speed; break; case PTZ_CMD_RIGHTDOWN: continuousMove.Velocity->PanTilt->x = speed; continuousMove.Velocity->PanTilt->y = -speed; break; case PTZ_CMD_ZOOM_IN: continuousMove.Velocity->PanTilt->x = 0; continuousMove.Velocity->PanTilt->y = 0; continuousMove.Velocity->Zoom->x = speed; break; case PTZ_CMD_ZOOM_OUT: continuousMove.Velocity->PanTilt->x = 0; continuousMove.Velocity->PanTilt->y = 0; continuousMove.Velocity->Zoom->x = -speed; break; default: break; } // 也可以使用soap_call___tptz__RelativeMove实现 result = soap_call___tptz__ContinuousMove(soap, ptzXAddr.c_str(), nullptr, &continuousMove, continuousMoveResponse); SOAP_CHECK_ERROR(result, soap, "ONVIF_PTZAbsoluteMove"); /* sleep(1); //如果当前soap被删除(或者发送stop指令),就会停止移动 ONVIF_PTZStopMove(ptzXAddr, ProfileToken);*/ EXIT: if (nullptr != soap) { ONVIF_soap_delete(soap); } return result; } void cb_discovery(char* deviceXAddr) { std::cout << "\n==== 发现设备: " << deviceXAddr << " ====" << std::endl; std::string ptzXAddr, profilesToken; // 获取设备能力信息 int ret = ONVIF_GetCapabilities(deviceXAddr, &ptzXAddr); if (ret != 0) { std::cerr << "错误: 获取设备能力失败 (错误码: " << ret << ")" << std::endl; return; } std::cout << " PTZ服务地址: " << ptzXAddr << std::endl; // 获取设备配置文件 ret = ONVIF_GetProfiles(ptzXAddr, &profilesToken); if (ret != 0) { std::cerr << "错误: 获取配置文件失败 (错误码: " << ret << ")" << std::endl; return; } std::cout << " Profile Token: " << profilesToken << std::endl; // 执行PTZ操作 ret = ONVIF_PTZContinuousMove(ptzXAddr, profilesToken, PTZ_CMD_LEFTUP, 0.3); if (ret != 0) { std::cerr << "错误: PTZ操作失败 (错误码: " << ret << ")" << std::endl; return; } // 停止PTZ操作 ret = ONVIF_PTZStopMove(ptzXAddr, profilesToken); if (ret != 0) { std::cerr << "错误: 停止PTZ操作失败 (错误码: " << ret << ")" << std::endl; } else { std::cout << " PTZ操作已停止" << std::endl; } } int main(int argc, char** argv) { ONVIF_DetectDevice(cb_discovery); return 0; } 获取配置文件失败
06-21
一、系统分层功能 感知层:用实验箱的两个传感器模块 传感器 1:温湿度传感器,采集温度、湿度数据。 传感器 2:红外对射 ,采集触发状态。 网络层: Zigbee 通信:传感器 1、2 的数据,经 Zigbee 节点传给协调器,协调器通过串口发 PC 端。 数据处理:解析串口数据,分主题发 MQTT 网络,或存数据库 。 应用层:用 Python 做终端,实现界面、串口、数据库交互(可加 MQTT),完成这些功能: 二、具体功能 数据上传: 远程设备实时收传感器 1 数据,格式:学号姓名缩写+温度+湿度 。 远程设备实时收传感器 2 数据,格式:学号姓名缩写+传感器名+interrupt 。 Mysql 数据库:建不同数据表存两个传感器数据,表含 学号姓名缩写、传感器名、数据值、传感器状态 等字段 。 命令下发: 发 学号姓名缩写+Num1Led+on ,传感器 1 连的 Zigbee 模块 LED2 亮;发 ...+off 则灭 。 发 学号姓名缩写+Num2Led+on ,传感器 2 连的 Zigbee 模块 LED2 亮;发 ...+off 则灭 。 数据联动: 传感器 1:温度>25 且湿度>60,其连的 Zigbee 模块 LED2 闪烁,远程设备、数据库表显示 “温湿度异常”;恢复后显示 “异常解除” 。 传感器 2:触发超 5 次,其连的 Zigbee 模块 LED2 闪烁,远程设备、数据库表显示 “传感状态异常”;恢复后显示 “传感异常解除” 。 强制解除:发 学号姓名缩写+Num1Led+relie ,传感器 1 连的 LED2 停闪,设备和表显示 “强制异常解除温湿度” ;发 ...+Num2Led+relie 同理。 数据展示:Python 界面里,串口控制,实时显示传感器 1 的 学号姓名缩写、温度、湿度 数据,呈现底层传感器联动状态 。 #include <stdio.h> #include <string.h> #include "OSAL.h" #include "ZGlobals.h" #include "AF.h" #include "aps_groups.h" #include "ZDApp.h" #include "MT_UART.h" //???? #include "SampleApp.h" #include "SampleAppHw.h" #include "OnBoard.h" /* HAL */ #include "hal_lcd.h" #include "hal_led.h" #include "hal_key.h" #include "hal_uart.h" #include "hal_mcu.h" #include "sht11.h" // P1 interrupt vector value is already defined in ioCC2530.h // #define P1INT_VECTOR 0x7B // Commented out to avoid redefinition warning // Sensor type configuration: // Combined sensor: supports both temp/humidity and IR sensor functions // sht11 command activates temp/humidity sensor, interrupt command activates IR sensor //#define COMBINED_SENSOR // Single sensor configuration (optional): //#define TEMP_HUMIDITY_SENSOR // Temperature/humidity sensor only // Default: IR sensor only (no macro defined) /********************************************************************* * CONSTANTS */ /********************************************************************* * TYPEDEFS */ /********************************************************************* * GLOBAL VARIABLES */ // This list should be filled with Application specific Cluster IDs. const cId_t SampleApp_ClusterList[SAMPLEAPP_MAX_CLUSTERS] = { SAMPLEAPP_PERIODIC_CLUSTERID, SAMPLEAPP_FLASH_CLUSTERID, SAMPLEAPP_BUZZER_CLUSTERID, SAMPLEAPP_SENSOR_IDENTIFY_CLUSTERID, }; // Add function declarations at the top of the file, after other declarations void SetBuzzerStatus(uint8 status); void SampleApp_ProcessMTMessage(afIncomingMSGPacket_t *msg); void InitIRSensorInterrupt(void); void ProcessIRSensorInterrupt(void); void ProcessLED2Flash(void); // LED2闪烁处理函数 void PrintDeviceInfo(void); // 打印设备信息 void PrintHexBytes(const char* title, const char* data, uint8 len); // 十六进制打印辅助函数 const SimpleDescriptionFormat_t SampleApp_SimpleDesc = { SAMPLEAPP_ENDPOINT, // int Endpoint; SAMPLEAPP_PROFID, // uint16 AppProfId[2]; SAMPLEAPP_DEVICEID, // uint16 AppDeviceId[2]; SAMPLEAPP_DEVICE_VERSION, // int AppDevVer:4; SAMPLEAPP_FLAGS, // int AppFlags:4; SAMPLEAPP_MAX_CLUSTERS, // uint8 AppNumInClusters; (cId_t *)SampleApp_ClusterList, // uint8 *pAppInClusterList; SAMPLEAPP_MAX_CLUSTERS, // uint8 AppNumOutClusters; (cId_t *)SampleApp_ClusterList // uint8 *pAppOutClusterList; }; // This is the Endpoint/Interface description. It is defined here, but // filled-in in SampleApp_Init(). Another way to go would be to fill // in the structure here and make it a "const" (in code space). The // way it's defined in this sample app it is define in RAM. endPointDesc_t SampleApp_epDesc; // Add global variable to control temperature and humidity reading static uint8 g_bReadTempHumi = 0; // Add IR sensor status variable static uint8 g_IRSensorStatus = 0; // Add sensor type variable - configure this based on device type // For combined sensor nodes, set to SENSOR_TYPE_COMBINED // For temperature/humidity sensor nodes, set to SENSOR_TYPE_TEMP_HUMIDITY // For IR sensor nodes, set to SENSOR_TYPE_IR #ifdef COMBINED_SENSOR static uint8 g_SensorType = SENSOR_TYPE_COMBINED; #elif defined(TEMP_HUMIDITY_SENSOR) static uint8 g_SensorType = SENSOR_TYPE_TEMP_HUMIDITY; #else static uint8 g_SensorType = SENSOR_TYPE_IR; #endif // 添加全局变量,用于防止中断处理函数重入 static uint8 g_IRProcessingInProgress = 0; // 添加控制LED2闪烁的全局变量 static uint8 g_LED2FlashStatus = 0; // 0: 停止闪烁, 1: 闪烁中 // 定义命令字符串常量,避免硬编码字符串和在函数调用中重复创建 const char* CMD_NUM1LED_ON = "42213238YFC+Num1Led+on"; const char* CMD_NUM1LED_OFF = "42213238YFC+Num1Led+off"; const char* CMD_NUM2LED_ON = "42213238YFC+Num2Led+on"; const char* CMD_NUM2LED_OFF = "42213238YFC+Num2Led+off"; const char* CMD_ON = "on"; const char* CMD_OFF = "off"; const char* CMD_HT = "ht"; const char* CMD_HF = "hf"; const char* CMD_FLASH = "flash"; // 闪烁命令 const char* CMD_RELIE = "42213238YFC+Num1Led+relie"; // 停止闪烁命令 const char* CMD_SHUO = "shuo"; // 红外对射传感器闪烁命令 const char* CMD_IR_RELIE = "42213238YFC+Num2Led+relie"; // 红外对射传感器停止闪烁命令 /********************************************************************* * EXTERNAL VARIABLES */ /********************************************************************* * EXTERNAL FUNCTIONS */ /********************************************************************* * LOCAL VARIABLES */ uint8 SampleApp_TaskID; // Task ID for internal task/event processing // This variable will be received when // SampleApp_Init() is called. devStates_t SampleApp_NwkState; uint8 SampleApp_TransID; // This is the unique message ID (counter) afAddrType_t SampleApp_Periodic_DstAddr; afAddrType_t SampleApp_Flash_DstAddr; aps_Group_t SampleApp_Group; uint8 SampleAppPeriodicCounter = 0; uint8 SampleAppFlashCounter = 0; /********************************************************************* * LOCAL FUNCTIONS */ void SampleApp_HandleKeys( uint8 shift, uint8 keys ); void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pckt ); void SampleApp_SendPeriodicMessage( void ); void SampleApp_SendFlashMessage( uint16 flashTime ); /********************************************************************* * NETWORK LAYER CALLBACKS */ /********************************************************************* * PUBLIC FUNCTIONS */ /********************************************************************* * @fn SampleApp_Init * * @brief Initialization function for the Generic App Task. * This is called during initialization and should contain * any application specific initialization (ie. hardware * initialization/setup, table initialization, power up * notificaiton ... ). * * @param task_id - the ID assigned by OSAL. This ID should be * used to send messages and set timers. * * @return none */ void SampleApp_Init( uint8 task_id ) { SampleApp_TaskID = task_id; SampleApp_NwkState = DEV_INIT; SampleApp_TransID = 0; // Device hardware initialization can be added here or in main() (Zmain.c). // If the hardware is application specific - add it here. // If the hardware is other parts of the device add it in main(). // Initialize IR sensor interrupt InitIRSensorInterrupt(); #if defined ( BUILD_ALL_DEVICES ) // The "Demo" target is setup to have BUILD_ALL_DEVICES and HOLD_AUTO_START // We are looking at a jumper (defined in SampleAppHw.c) to be jumpered // together - if they are - we will start up a coordinator. Otherwise, // the device will start as a router. if ( readCoordinatorJumper() ) zgDeviceLogicalType = ZG_DEVICETYPE_COORDINATOR; else zgDeviceLogicalType = ZG_DEVICETYPE_ROUTER; #endif // BUILD_ALL_DEVICES #if defined ( HOLD_AUTO_START ) // HOLD_AUTO_START is a compile option that will surpress ZDApp // from starting the device and wait for the application to // start the device. ZDOInitDevice(0); #endif // Setup for the periodic message's destination address // Broadcast to everyone SampleApp_Periodic_DstAddr.addrMode = (afAddrMode_t)AddrBroadcast; SampleApp_Periodic_DstAddr.endPoint = SAMPLEAPP_ENDPOINT; SampleApp_Periodic_DstAddr.addr.shortAddr = 0xFFFF; // Setup for the flash command's destination address - Group 1 SampleApp_Flash_DstAddr.addrMode = (afAddrMode_t)afAddrGroup; SampleApp_Flash_DstAddr.endPoint = SAMPLEAPP_ENDPOINT; SampleApp_Flash_DstAddr.addr.shortAddr = SAMPLEAPP_FLASH_GROUP; // Fill out the endpoint description. SampleApp_epDesc.endPoint = SAMPLEAPP_ENDPOINT; SampleApp_epDesc.task_id = &SampleApp_TaskID; SampleApp_epDesc.simpleDesc = (SimpleDescriptionFormat_t *)&SampleApp_SimpleDesc; SampleApp_epDesc.latencyReq = noLatencyReqs; // Register the endpoint description with the AF afRegister( &SampleApp_epDesc ); // Register for all key events - This app will handle all key events RegisterForKeys( SampleApp_TaskID ); MT_UartRegisterTaskID( SampleApp_TaskID ); //add by 1305106 // By default, all devices start out in Group 1 SampleApp_Group.ID = 0x0001; osal_memcpy( SampleApp_Group.name, "Group 1", 7 ); aps_AddGroup( SAMPLEAPP_ENDPOINT, &SampleApp_Group ); #if defined ( LCD_SUPPORTED ) HalLcdWriteString( "SampleApp", HAL_LCD_LINE_1 ); #endif // 打印设备类型和传感器类型信息 PrintDeviceInfo(); } /********************************************************************* * @fn SampleApp_ProcessEvent * * @brief Generic Application Task event processor. This function * is called to process all events for the task. Events * include timers, messages and any other user defined events. * * @param task_id - The OSAL assigned task ID. * @param events - events to process. This is a bit map and can * contain more than one event. * * @return none */ uint16 SampleApp_ProcessEvent( uint8 task_id, uint16 events ) { afIncomingMSGPacket_t *MSGpkt; (void)task_id; // Intentionally unreferenced parameter if ( events & SYS_EVENT_MSG ) { MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID ); while ( MSGpkt ) { switch ( MSGpkt->hdr.event ) { // Received when a key is pressed case KEY_CHANGE: SampleApp_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys ); break; // Received when a messages is received (OTA) for this endpoint case AF_INCOMING_MSG_CMD: SampleApp_MessageMSGCB( MSGpkt ); break;; case SPI_INCOMING_ZAPP_DATA: SampleApp_ProcessMTMessage(MSGpkt); MT_UartAppFlowControl (MT_UART_ZAPP_RX_READY); break; // Received whenever the device changes state in the network case ZDO_STATE_CHANGE: SampleApp_NwkState = (devStates_t)(MSGpkt->hdr.status); Sht11Init(); if ( (SampleApp_NwkState == DEV_ZB_COORD) || (SampleApp_NwkState == DEV_ROUTER) || (SampleApp_NwkState == DEV_END_DEVICE) ) { // Start sending the periodic message in a regular interval. HalLedSet(HAL_LED_1, HAL_LED_MODE_ON); osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT, SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT ); } else { // Device is no longer in the network } break; default: break; } osal_msg_deallocate( (uint8 *)MSGpkt ); MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID ); } return (events ^ SYS_EVENT_MSG); // return unprocessed events } // Send a message out - This event is generated by a timer // (setup in SampleApp_Init()). if ( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT ) { SampleApp_SendPeriodicMessage(); // Send the periodic message // Setup to send message again in normal period (+ a little jitter) osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT, (SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT + (osal_rand() & 0x00FF)) ); return (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT); // return unprocessed events } // Process IR sensor interrupt event if ( events & SAMPLEAPP_SEND_SENSOR_INT_EVT ) { ProcessIRSensorInterrupt(); return (events ^ SAMPLEAPP_SEND_SENSOR_INT_EVT); } // 处理LED2闪烁事件 if ( events & SAMPLEAPP_LED2_FLASH_EVT ) { // 添加调试输出 char buf[100]; sprintf(buf, "处理LED2闪烁事件, g_LED2FlashStatus=%d, g_SensorType=%d\r\n", g_LED2FlashStatus, g_SensorType); HalUARTWrite(0, (uint8*)buf, strlen(buf)); // 检查是否是温湿度传感器或红外对射传感器,且闪烁标志为1 if (g_LED2FlashStatus == 1 && (g_SensorType == SENSOR_TYPE_TEMP_HUMIDITY || g_SensorType == SENSOR_TYPE_IR)) { // 执行闪烁处理函数 ProcessLED2Flash(); // 不需要在这里设置下一次闪烁计时器,已在ProcessLED2Flash函数中处理 } else { // g_LED2FlashStatus不为1时,确保LED2关闭 if (g_LED2FlashStatus == 0) { HalLedSet(HAL_LED_2, HAL_LED_MODE_OFF); HalUARTWrite(0, (uint8*)"闪烁标志为0,停止闪烁并关闭LED2\r\n", 32); } } return (events ^ SAMPLEAPP_LED2_FLASH_EVT); } return 0; // Discard unknown events } /********************************************************************* * Event Generation Functions */ /********************************************************************* * @fn SampleApp_HandleKeys * * @brief Handles all key events for this device. * * @param shift - true if in shift/alt. * @param keys - bit field for key events. Valid entries: * HAL_KEY_SW_2 * HAL_KEY_SW_1 * * @return none */ void SampleApp_HandleKeys( uint8 shift, uint8 keys ) { (void)shift; // Intentionally unreferenced parameter if ( keys & HAL_KEY_SW_6 ) { /* This key sends the Flash Command is sent to Group 1. * This device will not receive the Flash Command from this * device (even if it belongs to group 1). */ SampleApp_SendFlashMessage( SAMPLEAPP_FLASH_DURATION ); } if ( keys & HAL_KEY_SW_2 ) { /* The Flashr Command is sent to Group 1. * This key toggles this device in and out of group 1. * If this device doesn't belong to group 1, this application * will not receive the Flash command sent to group 1. */ aps_Group_t *grp; grp = aps_FindGroup( SAMPLEAPP_ENDPOINT, SAMPLEAPP_FLASH_GROUP ); if ( grp ) { // Remove from the group aps_RemoveGroup( SAMPLEAPP_ENDPOINT, SAMPLEAPP_FLASH_GROUP ); } else { // Add to the flash group aps_AddGroup( SAMPLEAPP_ENDPOINT, &SampleApp_Group ); } } } /********************************************************************* * LOCAL FUNCTIONS */ /********************************************************************* * @fn SampleApp_MessageMSGCB * * @brief Data message processor callback. This function processes * any incoming data - probably from other devices. So, based * on cluster ID, perform the intended action. * * @param none * * @return none */ void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pkt ) { uint16 flashTime; uint8 *buf; switch(pkt->clusterId) { case SAMPLEAPP_PERIODIC_CLUSTERID: buf = pkt->cmd.Data; if(zgDeviceLogicalType == ZG_DEVICETYPE_COORDINATOR) { // Coordinator received data from end device if(pkt->cmd.DataLength == 4 && buf[0] <= 100) { // Format and print temperature data char tempStr[50]; sprintf(tempStr, "42213238YFC+温湿度传感器+温度:%d.%d C+湿度:%d.%d%%\r\n", buf[0], buf[1], buf[2], buf[3]); HalUARTWrite(0, (uint8*)tempStr, strlen(tempStr)); } else if(pkt->cmd.DataLength == 2 && buf[0] == 0x02) { // Process IR sensor data char irStr[50]; if(buf[1] == 0x01) { sprintf(irStr, "IR Sensor Node: Obstacle Detected!\r\n"); } else { sprintf(irStr, "IR Sensor Node: No Obstacle\r\n"); } HalUARTWrite(0, (uint8*)irStr, strlen(irStr)); } else if(pkt->cmd.DataLength == 3 && buf[0] == 0x03) { // Process interrupt message from end device if(buf[1] == 0x01) { // Coordinator prints the message and also blinks LED2 HalUARTWrite(0, (uint8*)"42213238YFC+红外对射传感器+interrupt\r\n", 36); // Flash LED2 10 times on coordinator as well HalLedBlink(HAL_LED_2, 10, 50, 200); } } else if(pkt->cmd.DataLength == 2 && buf[0] == 0x04) { // Process sensor identification response uint8 sensorType = buf[1]; char coordMsg[80]; uint16 srcAddr = pkt->srcAddr.addr.shortAddr; if(sensorType == SENSOR_TYPE_TEMP_HUMIDITY) { sprintf(coordMsg, "Device 0x%04X: Temperature/Humidity sensor identified and LED2 activated\r\n", srcAddr); HalUARTWrite(0, (uint8*)coordMsg, strlen(coordMsg)); } else if(sensorType == SENSOR_TYPE_IR) { sprintf(coordMsg, "Device 0x%04X: IR sensor identified and LED2 activated\r\n", srcAddr); HalUARTWrite(0, (uint8*)coordMsg, strlen(coordMsg)); } } } else { // End device received command from coordinator if(pkt->cmd.DataLength == 1) { if(buf[0] == 0x01) { // Start temperature reading g_bReadTempHumi = 1; osal_start_timerEx(SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT, 1000); } else { // Stop temperature reading g_bReadTempHumi = 0; osal_stop_timerEx(SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT); } } else if(pkt->cmd.DataLength == 2 && buf[0] == 0x10) { // 处理协调器转发的闪烁命令,仅终端设备响应 if(zgDeviceLogicalType != ZG_DEVICETYPE_COORDINATOR && g_SensorType == SENSOR_TYPE_TEMP_HUMIDITY) { if(buf[1] == 0x01) { // 启动LED2闪烁 g_LED2FlashStatus = 1; osal_set_event(SampleApp_TaskID, SAMPLEAPP_LED2_FLASH_EVT); // 添加调试输出 HalUARTWrite(0, (uint8*)"接收到闪烁命令,启动LED2闪烁\r\n", 30); } else { // 停止LED2闪烁 g_LED2FlashStatus = 0; HalLedSet(HAL_LED_2, HAL_LED_MODE_OFF); // 明确停止闪烁定时器 osal_stop_timerEx(SampleApp_TaskID, SAMPLEAPP_LED2_FLASH_EVT); // 添加调试输出 HalUARTWrite(0, (uint8*)"接收到停止命令,停止LED2闪烁\r\n", 30); } } } // 处理协调器转发的红外对射传感器闪烁命令 else if(pkt->cmd.DataLength == 2 && buf[0] == 0x11) { // 处理协调器转发的红外对射传感器闪烁命令,仅终端设备响应 if(zgDeviceLogicalType != ZG_DEVICETYPE_COORDINATOR && g_SensorType == SENSOR_TYPE_IR) { if(buf[1] == 0x01) { // 启动红外对射传感器LED2闪烁 g_LED2FlashStatus = 1; osal_set_event(SampleApp_TaskID, SAMPLEAPP_LED2_FLASH_EVT); // 添加调试输出 HalUARTWrite(0, (uint8*)"接收到红外对射传感器闪烁命令,启动LED2闪烁\r\n", 42); } else { // 停止红外对射传感器LED2闪烁 g_LED2FlashStatus = 0; HalLedSet(HAL_LED_2, HAL_LED_MODE_OFF); // 明确停止闪烁定时器 osal_stop_timerEx(SampleApp_TaskID, SAMPLEAPP_LED2_FLASH_EVT); // 添加调试输出 HalUARTWrite(0, (uint8*)"接收到红外对射传感器停止命令,停止LED2闪烁\r\n", 42); } } } } break; case SAMPLEAPP_FLASH_CLUSTERID: flashTime = BUILD_UINT16(pkt->cmd.Data[1], pkt->cmd.Data[2]); HalLedBlink(HAL_LED_4, 4, 50, (flashTime / 4)); break; case SAMPLEAPP_BUZZER_CLUSTERID: SetBuzzerStatus(pkt->cmd.Data[0]); break; case SAMPLEAPP_SENSOR_IDENTIFY_CLUSTERID: // Handle sensor identification requests if(zgDeviceLogicalType != ZG_DEVICETYPE_COORDINATOR) { // This is an end device, check if we should respond uint8 cmd = pkt->cmd.Data[0]; if(cmd == SENSOR_IDENTIFY_CMD_SHT11 && (g_SensorType == SENSOR_TYPE_TEMP_HUMIDITY || g_SensorType == SENSOR_TYPE_COMBINED)) { // This is a temperature/humidity sensor or combined sensor responding to sht11 command // Turn on LED2 and send identification response HalLedSet(HAL_LED_2, HAL_LED_MODE_ON); // Send identification response to coordinator uint8 response[2]; response[0] = 0x04; // Sensor identification response // For combined sensor responding to sht11, report as temp/humidity sensor response[1] = SENSOR_TYPE_TEMP_HUMIDITY; AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_PERIODIC_CLUSTERID, 2, response, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS); } else if(cmd == SENSOR_IDENTIFY_CMD_INTERRUPT && (g_SensorType == SENSOR_TYPE_IR || g_SensorType == SENSOR_TYPE_COMBINED)) { // This is an IR sensor or combined sensor responding to interrupt command // Turn on LED2 and send identification response HalLedSet(HAL_LED_2, HAL_LED_MODE_ON); // Send identification response to coordinator uint8 response[2]; response[0] = 0x04; // Sensor identification response // For combined sensor responding to interrupt, report as IR sensor response[1] = SENSOR_TYPE_IR; AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_PERIODIC_CLUSTERID, 2, response, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS); } else if(cmd == 0x05 && (g_SensorType == SENSOR_TYPE_TEMP_HUMIDITY || g_SensorType == SENSOR_TYPE_COMBINED)) { // Turn off LED2 for temperature/humidity sensors HalLedSet(HAL_LED_2, HAL_LED_MODE_OFF); } else if(cmd == 0x06 && (g_SensorType == SENSOR_TYPE_IR || g_SensorType == SENSOR_TYPE_COMBINED)) { // Turn off LED2 for IR sensors HalLedSet(HAL_LED_2, HAL_LED_MODE_OFF); } } break; } } /********************************************************************* * @fn SampleApp_SendPeriodicMessage * * @brief Send the periodic message. * * @param none * * @return none */ void SampleApp_SendPeriodicMessage(void) { if(g_bReadTempHumi) { uint8 temp_int, temp_dec, humi_int, humi_dec; char buf[50]; float humi, temp; // Initialize SHT11 sensor Sht11Init(); if(GetHumiAndTemp(&humi, &temp) == 0) { // Convert float to integer parts temp_int = (uint8)temp; temp_dec = (uint8)((temp - temp_int) * 10); humi_int = (uint8)humi; humi_dec = (uint8)((humi - humi_int) * 10); // Format the message sprintf(buf, "42213238YFC+温湿度传感器+温度:%d.%d C+湿度:%d.%d%%\r\n", temp_int, temp_dec, humi_int, humi_dec); // Send to UART HalUARTWrite(0, (uint8*)buf, strlen(buf)); // Prepare network data packet buf[0] = temp_int; buf[1] = temp_dec; buf[2] = humi_int; buf[3] = humi_dec; // Send data to network and check result afStatus_t status = AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_PERIODIC_CLUSTERID, 4, // Only send 4 bytes of data (uint8*)buf, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS); // 只记录错误但不中断流程,保持原有功能不变 if (status != afStatus_SUCCESS) { // 可以在这里添加错误处理代码,如重试或记录日志 // 但不要中断原有功能流程 } } // Restart timer for next reading osal_start_timerEx(SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT, 2000); // Read every 2 seconds } } /********************************************************************* * @fn SampleApp_SendFlashMessage * * @brief Send the flash message to group 1. * * @param flashTime - in milliseconds * * @return none */ void SampleApp_SendFlashMessage( uint16 flashTime ){ uint8 buffer[3]; buffer[0] = (uint8)(SampleAppFlashCounter++); buffer[1] = LO_UINT16( flashTime ); buffer[2] = HI_UINT16( flashTime ); if ( AF_DataRequest( &SampleApp_Flash_DstAddr, &SampleApp_epDesc, SAMPLEAPP_FLASH_CLUSTERID, 3, buffer, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS ) == afStatus_SUCCESS ) { } else { // Error occurred in request to send. } } void SampleApp_ProcessMTMessage(afIncomingMSGPacket_t *msg) { // 增加安全检查,确保msg指针有效 if(msg == NULL) { return; } const char *msgPtr = ((const char *)msg+2); uint8 status; // 调试输出,打印收到的命令 HalUARTWrite(0, (uint8*)"收到命令: ", 10); HalUARTWrite(0, (uint8*)msgPtr, strlen(msgPtr)); HalUARTWrite(0, (uint8*)"\r\n", 2); // 打印命令的十六进制值,帮助调试 PrintHexBytes("命令十六进制", msgPtr, strlen(msgPtr)); // 打印预期命令常量进行比较 char buf[100]; sprintf(buf, "预期relie命令: %s, 长度: %d\r\n", CMD_RELIE, strlen(CMD_RELIE)); HalUARTWrite(0, (uint8*)buf, strlen(buf)); // 打印预期命令的十六进制值 PrintHexBytes("预期十六进制", CMD_RELIE, strlen(CMD_RELIE)); // 使用更安全的字符串比较方式 if(strncmp(msgPtr, CMD_NUM1LED_OFF, strlen(CMD_NUM1LED_OFF)) == 0) { // Send command to turn off LED2 for temperature/humidity sensors uint8 cmd[2]; cmd[0] = 0x05; // LED control command cmd[1] = 0x00; // Turn off LED2 for temp/humidity sensors AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_SENSOR_IDENTIFY_CLUSTERID, 2, cmd, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS); } else if(strncmp(msgPtr, CMD_NUM2LED_OFF, strlen(CMD_NUM2LED_OFF)) == 0) { // Send command to turn off LED2 for IR sensors uint8 cmd[2]; cmd[0] = 0x06; // LED control command cmd[1] = 0x00; // Turn off LED2 for IR sensors AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_SENSOR_IDENTIFY_CLUSTERID, 2, cmd, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS); } else if(strncmp(msgPtr, CMD_ON, strlen(CMD_ON)) == 0) { status = 0x01; AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_BUZZER_CLUSTERID, 1, &status, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS); } else if(strncmp(msgPtr, CMD_OFF, strlen(CMD_OFF)) == 0) { status = 0x00; AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_BUZZER_CLUSTERID, 1, &status, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS); } else if(strncmp(msgPtr, CMD_HT, strlen(CMD_HT)) == 0) { // Forward the command to end device uint8 cmd = 0x01; // Command for starting temperature reading AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_PERIODIC_CLUSTERID, 1, &cmd, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS); } else if(strncmp(msgPtr, CMD_HF, strlen(CMD_HF)) == 0) { // Forward the command to end device uint8 cmd = 0x00; // Command for stopping temperature reading AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_PERIODIC_CLUSTERID, 1, &cmd, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS); } else if(strncmp(msgPtr, CMD_NUM1LED_ON, strlen(CMD_NUM1LED_ON)) == 0) { // Send sensor identification request for temperature/humidity sensors uint8 cmd = SENSOR_IDENTIFY_CMD_SHT11; AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_SENSOR_IDENTIFY_CLUSTERID, 1, &cmd, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS); } else if(strncmp(msgPtr, CMD_NUM2LED_ON, strlen(CMD_NUM2LED_ON)) == 0) { // Send sensor identification request for IR sensors uint8 cmd = SENSOR_IDENTIFY_CMD_INTERRUPT; AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_SENSOR_IDENTIFY_CLUSTERID, 1, &cmd, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS); } else if(strncmp(msgPtr, CMD_FLASH, strlen(CMD_FLASH)) == 0) { // 添加调试输出 HalUARTWrite(0, (uint8*)"收到flash命令\r\n", 14); // 只有温湿度传感器模块响应flash命令 if (g_SensorType == SENSOR_TYPE_TEMP_HUMIDITY) { // 添加调试输出 HalUARTWrite(0, (uint8*)"当前是温湿度传感器模块\r\n", 25); if (zgDeviceLogicalType != ZG_DEVICETYPE_COORDINATOR) { // 只有终端设备执行闪烁,协调器不闪烁 // 启动LED2闪烁 g_LED2FlashStatus = 1; // 触发闪烁事件,立即开始 osal_set_event(SampleApp_TaskID, SAMPLEAPP_LED2_FLASH_EVT); // 添加调试输出 HalUARTWrite(0, (uint8*)"终端设备开始LED2闪烁\r\n", 24); } else { // 协调器接收到flash命令时,转发给所有设备 uint8 cmd[2]; cmd[0] = 0x10; // 自定义的flash命令标识 cmd[1] = 0x01; // 开始闪烁 // 添加调试输出 HalUARTWrite(0, (uint8*)"协调器转发flash命令到网络\r\n", 27); AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_PERIODIC_CLUSTERID, 2, cmd, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS); } } else { // 添加调试输出 HalUARTWrite(0, (uint8*)"不是温湿度传感器模块,忽略命令\r\n", 32); } } else if(strncmp(msgPtr, CMD_RELIE, strlen(CMD_RELIE)) == 0) { // 添加调试输出 HalUARTWrite(0, (uint8*)"匹配到relie命令\r\n", 17); // 只有温湿度传感器模块响应relie命令 if (g_SensorType == SENSOR_TYPE_TEMP_HUMIDITY) { // 添加调试输出 HalUARTWrite(0, (uint8*)"当前是温湿度传感器模块\r\n", 25); if (zgDeviceLogicalType != ZG_DEVICETYPE_COORDINATOR) { // 只有终端设备执行停止闪烁,协调器不处理 // 停止LED2闪烁 g_LED2FlashStatus = 0; // 关闭LED2 HalLedSet(HAL_LED_2, HAL_LED_MODE_OFF); // 明确停止闪烁定时器 osal_stop_timerEx(SampleApp_TaskID, SAMPLEAPP_LED2_FLASH_EVT); // 添加调试输出 HalUARTWrite(0, (uint8*)"终端设备停止LED2闪烁\r\n", 24); } else { // 协调器接收到relie命令时,转发给所有设备 uint8 cmd[2]; cmd[0] = 0x10; // 自定义的flash命令标识 cmd[1] = 0x00; // 停止闪烁 // 添加调试输出 HalUARTWrite(0, (uint8*)"协调器转发停止闪烁命令到网络\r\n", 31); AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_PERIODIC_CLUSTERID, 2, cmd, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS); } } else { // 添加调试输出 HalUARTWrite(0, (uint8*)"不是温湿度传感器模块,忽略命令\r\n", 32); } } // 添加对红外对射传感器停止闪烁命令的处理 else if(strncmp(msgPtr, CMD_IR_RELIE, strlen(CMD_IR_RELIE)) == 0) { // 添加调试输出 HalUARTWrite(0, (uint8*)"匹配到红外对射停止闪烁命令\r\n", 29); // 只有红外对射传感器模块响应停止闪烁命令 if (g_SensorType == SENSOR_TYPE_IR) { // 添加调试输出 HalUARTWrite(0, (uint8*)"当前是红外对射传感器模块\r\n", 27); if (zgDeviceLogicalType != ZG_DEVICETYPE_COORDINATOR) { // 只有终端设备执行停止闪烁,协调器不处理 // 停止LED2闪烁 g_LED2FlashStatus = 0; // 关闭LED2 HalLedSet(HAL_LED_2, HAL_LED_MODE_OFF); // 明确停止闪烁定时器 osal_stop_timerEx(SampleApp_TaskID, SAMPLEAPP_LED2_FLASH_EVT); // 添加调试输出 HalUARTWrite(0, (uint8*)"红外对射终端设备停止LED2闪烁\r\n", 31); } else { // 协调器接收到停止闪烁命令时,转发给所有设备 uint8 cmd[2]; cmd[0] = 0x11; // 自定义的红外闪烁命令标识 cmd[1] = 0x00; // 停止闪烁 // 添加调试输出 HalUARTWrite(0, (uint8*)"协调器转发红外对射停止闪烁命令到网络\r\n", 38); AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_PERIODIC_CLUSTERID, 2, cmd, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS); } } else { // 添加调试输出 HalUARTWrite(0, (uint8*)"不是红外对射传感器模块,忽略命令\r\n", 34); } } // 尝试直接匹配红外对射传感器停止闪烁命令 else if(strcmp(msgPtr, "42213238YFC+Num2Led+relie") == 0) { // 添加调试输出 HalUARTWrite(0, (uint8*)"直接匹配到红外对射停止闪烁命令\r\n", 33); // 只有红外对射传感器模块响应停止闪烁命令 if (g_SensorType == SENSOR_TYPE_IR) { if (zgDeviceLogicalType != ZG_DEVICETYPE_COORDINATOR) { // 只有终端设备执行停止闪烁,协调器不处理 // 停止LED2闪烁 g_LED2FlashStatus = 0; // 关闭LED2 HalLedSet(HAL_LED_2, HAL_LED_MODE_OFF); // 明确停止闪烁定时器 osal_stop_timerEx(SampleApp_TaskID, SAMPLEAPP_LED2_FLASH_EVT); // 添加调试输出 HalUARTWrite(0, (uint8*)"红外对射终端设备直接停止LED2闪烁\r\n", 35); } else { // 协调器接收到停止闪烁命令时,转发给所有设备 uint8 cmd[2]; cmd[0] = 0x11; // 自定义的红外闪烁命令标识 cmd[1] = 0x00; // 停止闪烁 // 添加调试输出 HalUARTWrite(0, (uint8*)"协调器直接转发红外对射停止闪烁命令到网络\r\n", 42); AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_PERIODIC_CLUSTERID, 2, cmd, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS); } } } // 尝试另一种方式匹配温湿度传感器的relie命令(恢复原有逻辑) else if(strcmp(msgPtr, "42213238YFC+Num1Led+relie") == 0) { // 添加调试输出 HalUARTWrite(0, (uint8*)"直接匹配到温湿度传感器relie命令\r\n", 33); // 只有温湿度传感器模块响应relie命令 if (g_SensorType == SENSOR_TYPE_TEMP_HUMIDITY) { if (zgDeviceLogicalType != ZG_DEVICETYPE_COORDINATOR) { // 只有终端设备执行停止闪烁,协调器不处理 // 停止LED2闪烁 g_LED2FlashStatus = 0; // 关闭LED2 HalLedSet(HAL_LED_2, HAL_LED_MODE_OFF); // 明确停止闪烁定时器 osal_stop_timerEx(SampleApp_TaskID, SAMPLEAPP_LED2_FLASH_EVT); // 添加调试输出 HalUARTWrite(0, (uint8*)"温湿度传感器终端设备直接停止LED2闪烁\r\n", 39); } else { // 协调器接收到relie命令时,转发给所有设备 uint8 cmd[2]; cmd[0] = 0x10; // 自定义的flash命令标识 cmd[1] = 0x00; // 停止闪烁 // 添加调试输出 HalUARTWrite(0, (uint8*)"协调器直接转发温湿度传感器停止闪烁命令到网络\r\n", 46); AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_PERIODIC_CLUSTERID, 2, cmd, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS); } } } // 添加对shuo命令的处理 - 红外对射传感器LED闪烁命令 else if(strncmp(msgPtr, CMD_SHUO, strlen(CMD_SHUO)) == 0) { // 添加调试输出 HalUARTWrite(0, (uint8*)"收到shuo命令\r\n", 14); // 只有红外对射传感器模块响应shuo命令 if (g_SensorType == SENSOR_TYPE_IR) { // 添加调试输出 HalUARTWrite(0, (uint8*)"当前是红外对射传感器模块\r\n", 27); if (zgDeviceLogicalType != ZG_DEVICETYPE_COORDINATOR) { // 只有终端设备执行闪烁,协调器不闪烁 // 启动LED2闪烁 g_LED2FlashStatus = 1; // 触发闪烁事件,立即开始 osal_set_event(SampleApp_TaskID, SAMPLEAPP_LED2_FLASH_EVT); // 添加调试输出 HalUARTWrite(0, (uint8*)"红外对射终端设备开始LED2闪烁\r\n", 30); } else { // 协调器接收到shuo命令时,转发给所有设备 uint8 cmd[2]; cmd[0] = 0x11; // 自定义的红外闪烁命令标识,与温湿度传感器区分 cmd[1] = 0x01; // 开始闪烁 // 添加调试输出 HalUARTWrite(0, (uint8*)"协调器转发shuo命令到网络\r\n", 27); AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_PERIODIC_CLUSTERID, 2, cmd, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS); } } else { // 添加调试输出 HalUARTWrite(0, (uint8*)"不是红外对射传感器模块,忽略命令\r\n", 34); } } } // Initialize IR sensor interrupt function void InitIRSensorInterrupt(void) { // Set P1_2 as input pin P1DIR &= ~BV(2); // Configure P1_2 as input P1SEL &= ~BV(2); // Set as general I/O, not peripheral function // Configure P1_2 as pull-up input P1INP &= ~BV(2); // Set as tri-state input P2INP &= ~BV(5); // Clear P1 port pull-down setting, configure as pull-up // Read current status g_IRSensorStatus = (P1 & BV(2)) ? 0 : 1; // Set P1_2 as falling edge triggered PICTL |= BV(1); // P1 port uses falling edge trigger (PICTL.P1ICON = 1) // Enable P1_2 interrupt P1IEN |= BV(2); // Enable P1_2 interrupt function // Clear possible interrupt flags P1IFG &= ~BV(2); // Enable P1 port interrupt IEN2 |= BV(4); // Enable P1 port interrupt (IEN2.P1IE = 1) } // Process IR sensor interrupt function void ProcessIRSensorInterrupt(void) { // 简单的重入保护:如果已经在处理中,则退出 if(g_IRProcessingInProgress) { return; } g_IRProcessingInProgress = 1; // 设置处理中标志 // Read current status uint8 currentStatus = (P1 & BV(2)) ? 0 : 1; // For end devices, flash LED2 10 times and send interrupt message to coordinator if (zgDeviceLogicalType != ZG_DEVICETYPE_COORDINATOR) { // Flash LED2 10 times on end device only HalUARTWrite(0, (uint8*)"42213238YFC+红外对射传感器+interrupt\r\n", 36); HalLedBlink(HAL_LED_2, 10, 50, 200); uint8 buf[3]; buf[0] = 0x03; // Indicates this is an interrupt event message buf[1] = 0x01; // Interrupt occurred buf[2] = currentStatus; // Current sensor status AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_PERIODIC_CLUSTERID, 3, (uint8*)buf, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS); } // Status changed if (currentStatus != g_IRSensorStatus) { g_IRSensorStatus = currentStatus; // Send status to coordinator if (zgDeviceLogicalType != ZG_DEVICETYPE_COORDINATOR) { uint8 buf[2]; buf[0] = 0x02; // Indicates this is IR sensor data buf[1] = g_IRSensorStatus; AF_DataRequest(&SampleApp_Periodic_DstAddr, &SampleApp_epDesc, SAMPLEAPP_PERIODIC_CLUSTERID, 2, (uint8*)buf, &SampleApp_TransID, AF_DISCV_ROUTE, AF_DEFAULT_RADIUS); } } // Re-enable interrupt after processing // Clear interrupt flag P1IFG &= ~BV(2); // Re-enable interrupt P1IEN |= BV(2); g_IRProcessingInProgress = 0; // 清除处理中标志 } // LED2闪烁处理函数 void ProcessLED2Flash(void) { // 添加调试输出 HalUARTWrite(0, (uint8*)"执行LED2闪烁处理函数\r\n", 22); // 判断是温湿度传感器还是红外对射传感器 if (zgDeviceLogicalType != ZG_DEVICETYPE_COORDINATOR) { // 温湿度传感器模块执行闪烁 if (g_SensorType == SENSOR_TYPE_TEMP_HUMIDITY && g_LED2FlashStatus == 1) { // 添加调试输出 HalUARTWrite(0, (uint8*)"温湿度传感器符合条件,切换LED2状态\r\n", 35); // 切换LED2状态 (开/关) if ((HalLedGetState() & HAL_LED_2) == 0) { HalLedSet(HAL_LED_2, HAL_LED_MODE_ON); HalUARTWrite(0, (uint8*)"温湿度传感器LED2打开\r\n", 22); } else { HalLedSet(HAL_LED_2, HAL_LED_MODE_OFF); HalUARTWrite(0, (uint8*)"温湿度传感器LED2关闭\r\n", 22); } // 继续闪烁,500ms后再次触发 osal_start_timerEx(SampleApp_TaskID, SAMPLEAPP_LED2_FLASH_EVT, 500); } // 红外对射传感器模块执行闪烁 else if (g_SensorType == SENSOR_TYPE_IR && g_LED2FlashStatus == 1) { // 添加调试输出 HalUARTWrite(0, (uint8*)"红外对射传感器符合条件,切换LED2状态\r\n", 37); // 切换LED2状态 (开/关) if ((HalLedGetState() & HAL_LED_2) == 0) { HalLedSet(HAL_LED_2, HAL_LED_MODE_ON); HalUARTWrite(0, (uint8*)"红外对射传感器LED2打开\r\n", 24); } else { HalLedSet(HAL_LED_2, HAL_LED_MODE_OFF); HalUARTWrite(0, (uint8*)"红外对射传感器LED2关闭\r\n", 24); } // 继续闪烁,300ms后再次触发(红外传感器闪烁频率比温湿度传感器快一些) osal_start_timerEx(SampleApp_TaskID, SAMPLEAPP_LED2_FLASH_EVT, 300); } else { // LED2闪烁状态为0或传感器类型不匹配,不执行闪烁 if (g_LED2FlashStatus == 0) { HalUARTWrite(0, (uint8*)"闪烁状态为0,不执行LED2闪烁\r\n", 29); // 确保LED2处于关闭状态 HalLedSet(HAL_LED_2, HAL_LED_MODE_OFF); } else { // 传感器类型不匹配 HalUARTWrite(0, (uint8*)"传感器类型不匹配,不执行LED2闪烁\r\n", 33); } } } else { // 协调器不执行闪烁 HalUARTWrite(0, (uint8*)"协调器不执行LED2闪烁\r\n", 22); } } // Modify P1 interrupt handler related code, add P1 interrupt service routine HAL_ISR_FUNCTION(halP1Isr, P1INT_VECTOR) { if (P1IFG & BV(2)) { // Clear interrupt flag P1IFG &= ~BV(2); // Notify application to process interrupt via OSAL event osal_set_event(SampleApp_TaskID, SAMPLEAPP_SEND_SENSOR_INT_EVT); } // Clear P1 port interrupt flag P1IF = 0; } // Modify SetBuzzerStatus function to use another pin (P1_0) as buzzer output void SetBuzzerStatus(uint8 status) { if(status == 0x01) { P1_0 = 1; // Use P1_0 as buzzer control pin (active high) } else { P1_0 = 0; // Turn off buzzer } } void PrintDeviceInfo(void) { char buf[80]; // 打印设备类型信息 if(zgDeviceLogicalType == ZG_DEVICETYPE_COORDINATOR) { sprintf(buf, "设备类型: 协调器 (ZG_DEVICETYPE_COORDINATOR)\r\n"); } else if(zgDeviceLogicalType == ZG_DEVICETYPE_ROUTER) { sprintf(buf, "设备类型: 路由器 (ZG_DEVICETYPE_ROUTER)\r\n"); } else if(zgDeviceLogicalType == ZG_DEVICETYPE_ENDDEVICE) { sprintf(buf, "设备类型: 终端设备 (ZG_DEVICETYPE_ENDDEVICE)\r\n"); } else { sprintf(buf, "设备类型: 未知 (%d)\r\n", zgDeviceLogicalType); } HalUARTWrite(0, (uint8*)buf, strlen(buf)); // 打印传感器类型信息 if(g_SensorType == SENSOR_TYPE_TEMP_HUMIDITY) { sprintf(buf, "传感器类型: 温湿度传感器 (SENSOR_TYPE_TEMP_HUMIDITY)\r\n"); } else if(g_SensorType == SENSOR_TYPE_IR) { sprintf(buf, "传感器类型: 红外传感器 (SENSOR_TYPE_IR)\r\n"); } else if(g_SensorType == SENSOR_TYPE_COMBINED) { sprintf(buf, "传感器类型: 组合传感器 (SENSOR_TYPE_COMBINED)\r\n"); } else { sprintf(buf, "传感器类型: 未知 (%d)\r\n", g_SensorType); } HalUARTWrite(0, (uint8*)buf, strlen(buf)); // 打印编译时定义的宏 #ifdef TEMP_HUMIDITY_SENSOR sprintf(buf, "宏定义: TEMP_HUMIDITY_SENSOR 已定义\r\n"); #else sprintf(buf, "宏定义: TEMP_HUMIDITY_SENSOR 未定义\r\n"); #endif HalUARTWrite(0, (uint8*)buf, strlen(buf)); #ifdef COMBINED_SENSOR sprintf(buf, "宏定义: COMBINED_SENSOR 已定义\r\n"); #else sprintf(buf, "宏定义: COMBINED_SENSOR 未定义\r\n"); #endif HalUARTWrite(0, (uint8*)buf, strlen(buf)); } // 辅助函数,将字符串以十六进制格式打印出来,帮助调试 void PrintHexBytes(const char* title, const char* data, uint8 len) { char buf[200]; uint8 offset = 0; uint8 i; // 打印标题 offset = sprintf(buf, "%s: ", title); // 打印十六进制值 for (i = 0; i < len && offset < 190; i++) { offset += sprintf(buf + offset, "%02X ", (uint8)data[i]); } sprintf(buf + offset, "\r\n"); HalUARTWrite(0, (uint8*)buf, strlen(buf)); } /********************************************************************* *********************************************************************/ 根据要求和代码,实现相关功能,给出修改后的完整代码
06-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值