Necessary and sufficient optimality conditions for control of piecewise deterministic markov processes